欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 魔术方法简述

程序员文章站 2024-01-18 20:07:58
...
```name = $name; } public function getName() { return $this->name; } // 针对属性,设置值 public function __set($name, $value) { $this->$name = $value; } // 针对属性,读取值 public function __get($name) { if (isset($this->$name)) { return $this->$name; }else{ echo '尚未设置初始值'; } } // 针对方法,处理方法名和参数 public function __call($name, $arguments) { var_dump($arguments); return $name; } // 针对直接echo 类名的时候,调用__tostring()方法 public function __tostring() { return "this is a function tostring"; }}$student = new person('张三');echo $student; // this is a function tostring$student ->age = '李四';echo $student ->age; // 李四echo $student ->getAge('32');```