开发者

access overrided protected variable from parent class in PHP

开发者 https://www.devze.com 2023-02-27 03:05 出处:网络
Hi How can I access an overrided protected variable from parent class by calling its getter method in child class?

Hi

How can I access an overrided protected variable from parent class by calling its getter method in child class?

for example:

class A{
    protected x='test';
    protected function printx(){
        echo $this->x;
    }
}

class B extends A{
    protected x='test2';

    public function printxs(){
        parent::printx();
        echo "\n";
        echo $this->x;
    }
}

$b=new B;
$b->pr开发者_开发百科intsx();

I expect this print :

test
test2

but this prints:

test
test


Firstly, it doesn't print test\ntest, it prints test2\ntest2.

When you subclass a superclass you are specialising the superclass. In our example we are specialising the A class with the B class. Through that specialisation we're redefining the value of the protected object variable $this->x.

When you call the superclass' method printx() we are asked to echo the value of $this->x which has been redefined in our subclass to be test2, not test.

Here's the code PHPified:

<?php

class A {
    protected $x = 'test';
    protected function printx(){
        echo $this->x;
    }
}

class B extends A {
    protected $x = 'test2';

    public function printsx(){
        parent::printx();
        echo "\n";
        echo $this->x;
    }
}

$b=new B;
$b->printsx();


There is no such thing as "parent", here : there is only one property -- one memory slot for it.

Even if there property is first defined in the parent class, and, then, re-defined in the child class, as long as you are working with the property of an object ($this, here), it's always the same property.


Since your code doesn't compile here's an updated one:

<?php 

class A{

    protected $x='I am the value of Class A';

    public function getValueUsingAMethod() {

        return $this->x;

    }


}

class B extends A{

    protected $x='I am the value Class B';

    public function getValueUsingBMethod(){

        return $this->x;
    }

}

$anA = new A();
$aB = new B();

// Will output: B called - I am the value of Class A
echo '<br />B called - ' . $anA->getValueUsingAMethod();

// Will output: A called - I am the value Class B
echo '<br />A called - ' . $aB->getValueUsingAMethod();

// Will output: B called - I am the value Class B
    echo '<br />B called - ' . $aB->getValueUsingBMethod();

// Outputs this
//   object(B)#2 (1) { ["x":protected]=> string(22) "I am the value Class B" } 
var_dump( $aB );

Have a look at the second line of output. You call a method a class A and the method returns the value from the object instance of class B.

If you subclass A by means of a class B and B overwrites a variable in A's scope, all of A's methods automatically access the overwritten variables if they are called from an instance of B.

The last line of output describes the internal structure of B. As you see, only a single instance variable x is available.

Why?

If you overwrite $x, the semantics is 'Use my new $x instead of the origial $x'.

If you definitely need to access A's $x, you might wish to create a differently named additional member variable in B.

0

精彩评论

暂无评论...
验证码 换一张
取 消