/ Published in: PHP
OOP style getters and setters in PHP.
Expand |
Embed | Plain Text
<?php class Person { private $first; private $last; public setName ($name) { } public getName ($name) { } public function __set ($name, $value) { $setter = 'set' . $key; $this->$setter($value); return $value; } trigger_error('Undefined property via ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE); } public function __get ($name) { $getter = 'set' . $key; return $this->$getter(); } trigger_error('Undefined property via ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE); } } ?>
Comments
Subscribe to comments
You need to login to post a comment.

I can't quite see the point of all that, performing string operations on function names isn't really going to be terribly performant when. Better to just make up individual get/sets for each variable re http://www.icurtain.co.uk/getset.php