Question

This is my first question, and something which has got me stumped. I'm not sure if this is something simple and I'm overlooking it or something not possible.

Below is a very simplified version of my original code. The end goal is to have the output as follows:

1: 
2: this is a test
3: this is another test
4: this is another test

However, with the code in its current state, its actual output is this:

1: 
2: this is a test
3: this is another test
4: 

I want object 'B' to be able to access the value of test_variable AFTER first_function() has altered it.

It works fine when I declare test_variable as static, however in the actual application it wouldn't work and when I try to echo parent::test_variable it outputs 'Object ID #17' and so on.

class A
{
    public $test_variable;

    function __construct()
    {
        echo '1: ' . $this->test_variable . "<br />";
        $this->test_variable = 'this is a test';
        echo '2: ' . $this->test_variable . "<br />";
    }

    function first_function()
    {
        $this->test_variable = 'This is another test';
        echo '3: ' . $this->test_variable . "<br />";
        $b = new b;
        $b->second_function();
    }
}



class B extends A
{
    function __construct()
    {
        /* Dont call parent construct */
    }

    function second_function()
    {
        echo '4: ' . $this->test_variable;
    }
}

$a = new A;
$a->first_function();

// Outputs:
// 1:
// 2: this is a test
// 3: this is another test
// 4: 

// but I want it to output
// 1: 
// 2: this is a test
// 3: this is another test
// 4: this is another test

Many thanks for any responses. I greatly appreciate them.

Phil

Was it helpful?

Solution

Declaring public $test_variable; inside the class means each instance (object) of the class has a copy. $test_variable in class A does not point to the same memory address as $test_variable in Class B. This is done intentionally to allow scope and remove a global state. As you said before, declaring it static will work, because then each instance shares the same variable.

In this instance, $test_variable is, in essence, a dependency that class B requires. You can get that dependency via constructor injection fairly easily:

class A
{
    public $test_variable;

    function __construct()
    {
       echo '1: ' . $this->test_variable . "<br />";
       $this->test_variable = 'this is a test';
       echo '2: ' . $this->test_variable . "<br />";
    }

    function first_function()
    {
       $this->test_variable = 'This is another test';
       echo '3: ' . $this->test_variable . "<br />";

       // Instantiate instance passing dependency
       $b = new b($this->test_variable);

       $b->second_function();
    }
}

class B extends A
{
    function __construct($dependency)
    {
       // Set dependency
       $this->test_variable = $dependency;
    }

    function second_function()
    {
       echo '4: ' . $this->test_variable;
    }
}

$a = new A;
$a->first_function();

So, that's just a thought on how you might consider handling this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top