What I would like to do is chain a few classes together using "extends". It kinda works, but not like I expected it to. So I must be doing something wrong.
From what I gathered, when you extend a class you inherit all of its properties. So with that in mind I like to make my config file available in all classes. To do this I want to extend all classes on an autoLoader class. This seems logical to me, but correct me if I'm wrong.
I have two classes
AutoLoader
Class AutoLoader {
public $oConf = null;
public function __construct($oConfig) {
$this->$oConf = $oConfig;
}
public function getConf() {
return $this->$oConf;
}
}
other class
Class foo extends AutoLoader {
public function bar() {
var_dump($this->getConf());
}
}
My question is, why is the var_dump NULL.
It is strange to me cause this always returns the conf:
$autoLoader = new AutoLoader($array);
var_dump($autoLoader->getConf());
What am I missing here.
You are extending and inheriting the class structure, not the object instance.
Just take this code as an example:
$foo = new Foo('foo');
$bar = new Foo;
Would you expect $bar to have access to the argument 'foo' passed into the first object somehow? No. Extension doesn't change that.
You should be using composition or dependency injection (e.g. pass an autoloader into the constructor of Foo). Don't make Foo an Autoloader, give it an Autoloader if it needs one. When you extend a class, that new class is also its parent class. That means it could be used in place of the parent class. Anywhere you'd use an Autoloader class, you could substitute Foo instead. Does that make sense? Is Foo an Autoloader? Does $foo instanceof Autoloader == true make any sense? No.
If inheriting class have an own constructor, you are probably not passing the oConfig to the parenting class (Autoloader) in the case.
also, your constructor expects an config.
So you should be doing this in your child constructor if you don't have one:
function __construct($oConfig) {
parent::__construct($oConfig);
// rest of your constructor code here
}
You should create instance of foo class and give into constructor your config variable:
$config = 'My_config';
$foo = new foo($config);
$foo->bar();
Will output:
(string) "My_config"
Related
Background
In a project with a PHP 5.6 runtime, I need to work around some third-party code. This code will not be changed by the vendor, nor will it be removed from the codebase.
Specifically, the third-party code (let's call its namespace Theirs) contains a class (\Theirs\BaseClass) whose constructor instantiates another class (\Theirs\Detector).
BaseClassTheirs.php:
<?php namespace Theirs;
class Detector {
public function __construct() {
print "Theirs\n";
}
}
class BaseClass {
public function __construct() {
$detector = new Detector();
}
}
I do not want BaseClass to instantiate \Theirs\Detector. Instead, I want BaseClass to instantiate a different Detector class, from a different namespace (Mine) that is outside of the third-party's control.
In all other respects, though, I want BaseClass to behave as it does in the third-party code, including if the vendor later adds additional functionality to \Theirs\BaseClass. (I'll call this property "non-fragility" and the lack of it "fragility".) As such, it seems sensible for me to create my own \Mine\BaseClass as a child of \Theirs\BaseClass, inheriting everything from it.
If I take the fragile, non-DRY approach of copy-pasting \Theirs\BaseClass's constructor into \Mine\BaseClass, then \Mine\Detector is instantiated, as I desired:
BaseClassMine.php:
<?php namespace Mine;
include "BaseClassTheirs.php";
class Detector {
public function __construct() {
print "Mine\n";
}
}
class BaseClass extends \Theirs\BaseClass {
public function __construct() {
$detector = new Detector();
}
}
\\ Prints "Mine"
$obj = new BaseClass();
However, if I change this into a DRY, non-fragile approach by removing the duplicated code so that \Mine\BaseClass invokes exactly the same constructor, but as inherited from its parent rather than being copy-pasted, then \Theirs\Detector gets invoked, which is not what I want:
BaseClassMine.php:
<?php namespace Mine;
include "BaseClassTheirs.php";
class Detector {
public function __construct() {
print "Mine\n";
}
}
use \Mine\Detector;
class BaseClass extends \Theirs\BaseClass {
}
\\ Prints "Theirs"
$obj = new BaseClass();
This happens regardless of whether the file contains a use \Mine\Detector; line, as above.
Question
How can I get the best of both approaches?
I.e. how can I invoke \Theirs\Baseclass's constructor from \Mine\Baseclass's constructor in order to have it invoke \Mine\Detector, as though \Theirs\Baseclass's constructor had simply been copy-pasted into \Mine\Baseclass's context, but without actually copy-pasting it and introducing the corresponding fragility?
For instance, is there a good way to use reflection or some other introspective technique to dynamically read the parent's constructor and to "paste" it at runtime into the child class?
Related but not identical questions
late static binding | without modifying parent class with static keyword
Is there any way to set a property before calling a constructor?
I have an object with some protected fields and a method that uses them. The method doesn't do exactly what I need it to do, but I cannot change the original code since I am writing an add-on.
Is it somehow possible to extend the class and override the method so that I could call it on predefined objects of the original class? I thought about monkey patching but apparently it is not implemented in php.
You can override a method by extending the parent class, initiating the new class instead of the parent class and naming your method exactly the same as the parent method, that was the child method will be called and not the parent
Example:
class Foo {
function sayFoo() {
echo "Foo";
}
}
class Bar extends Foo {
function sayFoo() {
echo "Bar";
}
}
$foo = new Foo();
$bar = new Bar();
$foo->sayFoo() //Outputs: Foo
$bar->sayFoo() //Outputs: Bar
I hope below stategy will be works. asume that class is Foo and method is bar(). for override bar() method you have to make customFoo class as mentioned below.
class CustomFoo extends Foo{
public function bar(){
parent::bar();
}
}
I dont know actually what you need because you dont have explained in detail. Still I have tried my best. :)
Try creating a child class that extends the base or parent class that the object currently derives from.
Create a new method with exactly the same name as the method in the Parent class and put your logic in there.
Now instantiate your object from your new class, you would have succeeded in overriding that particular method and still have access to the methods and properties of the base class.
Problem is, once you've loaded the class, you can't officially unload it, and you do need to load it in order to extend it. So it's pretty tied up. Your best bet is to either hack the original class (not ideal) or copy paste the original class definition into a new file:
class ParentClass {
//Copy paste code and modify as you need to.
}
Somewhere after the bootstrapping of your framework:
spl_autoload_register(function ($class) {
if ($class == "ParentClass") { //Namespace is also included in the class name so adjust accordingly
include 'path/to/modified/ParentClass.php';
}
},true,true);
This is done to ensure your own modified class will be loaded before the original one.
This is extremely hacky so first check if the framework you're using has native support for doing this.
Suppose I have,
interface Foo {
}
class FooClass implements Foo {
}
Is there a way I can add some logic to the interface outside of the FooClass so that I can do this:
$foo = new FooClass();
$foo->Bar();
where Bar is a method applicable to all Foos? What I am asking for is something similar to extension methods in C# but in my case I want to extend just interfaces.
I know I can add a custom static function Bar which accepts Foo as parameter but I want to know if there's anything in PHP which gives me the $foo->Bar() sugar.
Edit: I think I wasn't clear enough. I have a couple of similar interfaces (and many classes implementing them) from an external library. Changing them to abstract classes is not an option for me.
I think the closest you can get with PHP are the so called traits.
<?php
interface Foo {
}
trait myTraits {
function bar() { echo "BarMethod"; }
}
class FooClass implements Foo {
use myTraits;
}
$foo = new FooClass();
$foo->Bar();
?>
This will output "BarMethod".
I ran into an issue with parent/child classes in different namespaces that I'm wondering if it's my improper use, or if this is a quirk of PHP namespaces:
<?php
namespace myVendorName;
class MyParentClass {
protected $args = array();
function factory($class) {
return new $class($this->args);
}
}
?>
<?php
namespace myVendorName\subPackage;
class foo {
}
class bar extends \myVendorName\MyParentClass {
function myFunc() {
var_dump(new foo()); // Works (we're in the same namespace)
var_dump($this->factory('foo')); // Doesn't work (no such class as myVendorName\foo)
var_dump($this->factory('subPackage\foo')); // Works
}
}
?>
The above code doesn't work as I'd expect. In the \myVendorName\subPackage\bar->myFunc() method, I'd like to get a \myVendorName\subPackage\foo class. If I just create it there, I can create it by only its class name since the namespace is the same. However, those classes in reality are more complex, and there's a factory method to create them. The factory method is universal, and is defined in the root vendor namespace. And the bar class doesn't overwrite that factory method; it simply inherits it. However, when referring to a class object by name, it seems to still operate in the parent's namespace, rather than having the method truly get inherited by the child and operate in the child's namespace.
Is there a way for a parent class method that's going to be inherited directly to use the child's namespace, or at least peek at what it is? Or does each of my child classes have to overwrite the factory method to fix the namespace, and call the parent method within themselves?
The answer, as Passerby indicated is that PHP does have a __NAMESPACE__ constant that is filled with the current namespace (though no leading slash). So modifying the factory function to:
function factory($class) {
if ($class[0] != '\\') {
$class = '\\'.__NAMESPACE__.$class; // If not a fully-qualified classname, prepend namespace
}
return new $class($args);
}
works as expected (there's an example of this in the PHP documentation)
So I have a config.php file, wich is included on top of every page on the site before the tag. This file uses __autoload() to get all the php classes I use. Then, after autoloading them I assign the classes to variables like so...
$myclass = new Myclass();
$mysecondclass = new MySecondClass();
When I want to call $mysecondclass in $myclass, I get an undefined variable error. This is of course, because $mysecondclass was not defined before $myclass. How do I fix this where I can define all classes from any other class?
Thanks for any help:)
The best OOP approach would be to use a superObject with properties which are objects of other classes.
class Context{
var $myClass;
var $myOtherClass;
var $db;
//...
}
In your MyClass
class MyClass{
var $context;
public function __construct(&context){
$this->context = $context;
}
public function otherFunction(){
$this->context->myOtherClass->functionFromOtherClass();
}
}
You should be instantiating these classes using Factory method or any mechanism to manage objects.
To initialize MyClass you would implement something like this.
ObjectFactory::createObject('MyClass',$context);
$myclass is not Myclass it is an instance of Myclass (an object). Objects are not classes. If you want to use a new instance of MySecondClass inside of an instance of Myclass, just instantiate it in there.
Your question also deals with scope. You have created these two objects in the global scope. They are not automatically accessible within one another. If you have some object which you will have only one instance of and which you want to be global and accessible by others, you can import it into their scope with the global keyword. However, this is not usually the best way to go about it. Instead, you may want to look up the Singleton Pattern. It gives you a way to ensure that a particular class is only instantiated one time and gives you a way to access the object that has been created as a result of that instantiation.
The registry pattern is also an easy way to access "global" object anywhere in your code.
You can Take a look at Zend_Registry : http://framework.zend.com/manual/en/zend.registry.using.html
$myclass = new Myclass();
$mysecondclass = new MySecondClass();
Zend_Registry::set('mysecondclass', $mysecondclass);
$myclass->doSomething();
class Myclass
{
public function doSomething()
{
// use $mysecondclass via the registry
Zend_Registry::get('mysecondclass')->methodFromSecondClass();
}
}
class MySecondClass
{
public function methodFromSecondClass()
{
return true;
}
}
According to how your classes works, think about extending them... Extending MyClass to MySecondClass automatically gives access to parent properties / functions.
MyClass
class MyClass{
function myFunction(){
// code here
}
}
MySecondClass
class MySecondClass extends MyClass{
function __construct(){
$this->myFunction();
}
}