Yousuf Memon
Yousuf Memon

Reputation: 4798

Late static bindings

Do we need to use the static::$attribute instead of $this->attribute in the following condition :

b.php

class B {
public function tellAttribute(){
// $this OR static ??
    echo $this->attribute;
}
}

a.php

include 'b.php';

class A extends B {
public $attribute = 'foo';
}

$test = new A();
$test->tellAttribute();

Asking this because to me it shouldn't work unless I use static::$attribute but its still echoing foo. What is the reason?

Upvotes: 1

Views: 167

Answers (4)

pb149
pb149

Reputation: 2298

Class B defines a public function called tellAttribute() that looks like this:

public function tellAttribute(){
    echo $this->attribute;
}

You then instantiate class A - a child of class B - and do this:

$test = new A();
$test->tellAttribute();

So, you instantiate an object of class A and then call tellAttribute() on this object. Because the tellAttribute() method uses the $this variable you are referring to the actual object you have instantiated. Even though you defined tellAttribute() in class B - the parent - it will actually be pointing to the child object (an instance of class A) in which you have the public $attribute property. That's why it prints foo and why you don't need to use static::.

On the other hand, consider this:

class B {
    public static $attribute = 'foo';
    public function tellAttribute(){
        echo self::$attribute; // prints 'foo'
    }
    public function tellStaticAttribute() {
        echo static::$attribute; // prints 'bar'
    }
}

class A extends B {
    public static $attribute = 'bar';
}

$test = new A();
$test->tellAttribute();
print "<BR>";
$test->tellStaticAttribute();

In this example, I'm not using the $this variable and am instead using self:: and static::. The tellAttribute() has self:: and will always print foo. This is because self:: can only refer to the current class. The tellStaticAttribute() uses static:: and will print the class 'dynamically'. I'm not too great on the technical terms etc. so I'll leave you with a link to the manual (which I gather you have already read from your post): http://php.net/manual/en/language.oop5.late-static-bindings.php

Hope that answers your question.

Upvotes: 2

DaOgre
DaOgre

Reputation: 2100

You aren't using the "static" keyword anywhere. Your general class setup here is a little odd to me, but if you want $attribute to be a static variable you need to instead type:

 public static $attribute = 'foo';

Keep in mind a static variable is basically a global variable, and you can review more here: http://php.net/manual/en/language.oop5.static.php

Upvotes: 0

Mahn
Mahn

Reputation: 16603

Do we need to use the static::$attribute instead of $this->attribute in the following condition : < code >

No, you most definitively don't use the static keyword in the scenario you describe, and there's no reason for it not to work. Think of the context of $this as the result of "adding up" all the different inherited classes into one. That is to say, if class B extends A, and class C extends B, by instantiating C all the properties and functions of the classes A, B and C are available through the $this context within the class, and it's perfectly fine for C to use within a function of its own a property defined in B and viceversa, because everything is there as if it were one standalone class in your instance.

Upvotes: 1

Baba
Baba

Reputation: 95161

Yes it would work .... $attribute is public ..... and A has also inherited tellAttribute() am not sure what you where expecting.

Upvotes: 0

Related Questions