开发者

parent:: in instantiated classes

开发者 https://www.devze.com 2022-12-16 11:56 出处:网络
i was wondering why there is no $parent->function(); syntax in php, but instead we can use parent::functio开发者_StackOverflow中文版n(); which looks like it\'s used inside a static class. Am i miss

i was wondering why there is no $parent->function(); syntax in php, but instead we can use parent::functio开发者_StackOverflow中文版n(); which looks like it's used inside a static class. Am i missing some php oop basics?


I admit it seems strange -- and you didn't miss anything in the manual ^^

But :

  • Generally, when the child class re-defines a method that's already defined in the parent class, you want the child's method to totally override the parent's one
    • except for __construct, I admit -- that's probably why it's said explicitly in the manual that you have to call the parent's __construct method yourself.
  • Generally speaking, when working with non-static methods, you'll just use $this to call methods in the same instance of either the child or the parent class ; no need to know where the method actually is.
  • Using parent:: works fine, even if it looks like a static call


And here's an example of code showing parent:: works fine :

class Father {
    public function method() {
        var_dump($this->a);
    }
}

class Son extends Father {
    protected $a;
    public function method() {
        $this->a = 10;
        parent::method();
    }
}

$obj = new Son();
$obj->method();


You'll get this output :

$ /usr/local/php-5.3/bin/php temp.php
int(10)

Which shows that the method in the parent class has access to $this and the properties defined in the child class.


Well, parent actually references the static parent class - there is no reason to assume there is an instantiated $parent only because there exists a $child, and even if there were, $child would not have access to $parent.

Finally, an instance where the usual class dog extends animal OOP explanations don't work! :)


Because using $parent assumes that you have actually instantiated the parent class.

If your syntax suggest worked, it would mean everytime you instantiated one object, you were instantiating 2 or more objects.

In PHP, every variable must contain a string, integer(or other numeric format), array, object, or resource. $this contains an object, and it just happens to be the object that you are currently inside.

In order to create $parent, you would have to put an object inside $parent. You parent class is technically not instantiated, so it cannot be assigned to a variable.

BTW parent::function(); has access to all of $this.

Hence, this works

class Test
{
    public function test()
    {
        echo $this->testing_var;
    }
}

class OtherTest
{
    public function run()
    {
        $this->testing_var = "hi";
        Test::test(); // echos hi
    }
}

And this will error if it is used outside a class, and will tell you it should be declared static.

Test::test();
0

精彩评论

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