<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
class Person {
private $data;
public function __construct($data) {
$this->data = $data;
}
public function __get($prop) {
return $this->data[$prop];
}
}
$data = array('name' => 'Mark');
$person = new Person($data);
echo $person->get('name');
?>
不应该工作吗?我得到了Fatal error: Call to undefined method Person::get()
,但我不知道为什么。根据magic方法的文档,这段代码应该可以正常工作。为什么我的getter不工作?
http://php.net/manual/en/language.oop5.overloading.php#object.get
有没有线在手动中说$ person-> get()调用__get()方法:)另外,必须调用get()会使“魔法方法”定义变得毫无意义 – 2014-10-30 11:27:29