<?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');
?>
Shouldn't this work?
I get Fatal error: Call to undefined method Person::get()
, but I don't know why. As per the documentation for the magic methods, this code should work fine.
http://php.net/manual/en/language.oop5.overloading.php#object.get