<?phpclass Staff{ private $name; private $age; private $salary; public function _construct($name,$age,$salary) { $this->name=$name; $this->age=$age; $this->salary=$salary; } public function _get($name) { return $this->$name; }}$obj=new Staff('peter',18,4000);echo $obj->name;echo $obj->age;?>
卢小强2019-11-09 08:41:11
If you want to output $name, you must use the public function _get method. Private is a private variable and cannot be called externally.
永夜微尘2019-11-08 18:52:23
The properties in the Staff class are defined as private and can only be called within the class. Change them to public and solve the problem