parent:: in instantiated classes
php
Solution
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.
Problem
i was wondering why there is no `$parent->function();` syntax in php, but instead we can use `parent::function();` which looks like it's used inside a static class. Am i missing some php oop basics?