0

Ok, so, with the code below:

class Core {
    public $child;
    public function start() {
        $child = Loader::instance('Child');
        print_r($this);
    }
}

class Loader extends Core {
    public static $instances;

    public static function instance($class) {
        if(!isset(self::$instances[$class])) {
            self::$instances[$class] = new $class();
        }

        return self::$instances[$class];
    }
}

class Child extends Core {
    public function __construct() {
        parent::__construct();

        $this->child = 'test';
    }
}

Loader::instance('Core')->start();

I should be able after a print_r($this) to see:

Core Object
(
    [child] => test
)

instead of

Core Object
(
    [child] => 
)

what is happening now?

Thanks again..

3 Answers 3

2

Within the start() method of Core class you use variable $child, that is not the property of the object. Instead write:

class Core {
    public $child;
    public function start() {
        $this->child = Loader::instance('Child')->child;
        print_r($this);
    }
}

and tell me, whether this is what you wanted to accomplish.

EDIT:

I believe you can achieve what you want by referencing static variables. But be careful, how they work. Moreover, you will not see the result by invoking print_r().

Here is the code:

class Core {
    static $child;
    public function start() {

        // invoking code that changes Core::$child inside
        $child = Loader::instance('Child');

        print_r($this);
    }
}

and

class Child extends Core {
    public function __construct() {
        parent::__construct();

        // changing static variable $child of both Core and Child
        self::$child = 'test';
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

codeclass Child extends Core { public function __construct() { parent::__construct(); $this->variable = 'test'; } }
@Gabriel I have updated answer (added ->child). Does it work for you now?
Yes.. now my question is: if I do the follow class Child extends Core { public function __construct() { parent::__construct(); $this->child = 'test'; } } I can't auto-update Core->child variable with 'test' value?
In that case object of class Child is the instance of Core class. In these terms you are already doing it. But if you are talking about $child being instance of class Child and $core being instance of class Core, then you have to separately change the value of $core->child. It is not done automatically, as object of class Core is not the same as object of class Child. They are only similar in types. This is basic understanding of how classes work.
I have updated the answer - you may be looking for static variables, but you should know how they work. And they won't be displayed when the print_r is invoked.
|
0
$child = Loader::instance('Child');

Shoulld be:

$this->child = Loader::instance('Child');

Comments

0

This is where your print_r output comes from:

    $child = Loader::instance('Child');
    print_r($this);

And it will print the current object ($this is a Core), rather than the newly instantiated $child (which would be a Child class).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.