I want to call the private property of the subclass in the parent class, but I'm reporting a 500 error.
class A{ public function __get($name) { $getter = 'get' . $name; if (method_exists($this, $getter)) { return $this->$getter(); } }}class B extends A{ private function getname() { return 'karly'; }}$b = new B();echo $b->name;
This code runs after the server reported 500 error, why call method_exists return the result is true, still can't return the method? Thank you.
Reply content:
I want to call the private property of the subclass in the parent class, but I'm reporting a 500 error.
class A{ public function __get($name) { $getter = 'get' . $name; if (method_exists($this, $getter)) { return $this->$getter(); } }}class B extends A{ private function getname() { return 'karly'; }}$b = new B();echo $b->name;
This code runs after the server reported 500 error, why call method_exists return the result is true, still can't return the method? Thank you.
First, according to the official PHP document:
-
Public represents the global, and the inner and outer subclasses of the class can be accessed;
-
Private means that only this class can be used internally;
-
Protected is protected and is accessible only in this class or subclass or in the parent class;
Secondly, the meaning of method_exists is whether the test exists, the problem is that people really exist, but there is no authority, which is no problem.
You should use it here.protected
Private member methods seem to only invoke themselves