search

Home  >  Q&A  >  body text

What's wrong with what I wrote? Please help me check it out.

<?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;?>微信截图_20191108132128.png

LaurieLaurie1878 days ago1212

reply all(2)I'll reply

  • 卢小强

    卢小强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.

    reply
    0
  • 永夜微尘

    永夜微尘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

    reply
    0
  • Cancelreply