<?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;?>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你要想输出$name得用 public function _get方法调用,private是私有变量,外部调用不了。
Staff 类中的属性定义为私有 private,只能在类内部调用,改为public,问题解决