note 97572 added to language.oop5.overloading
| From: | zzzzbov at osu1 dot php dot net | Date: | Mon, 26 Apr 2010 19:31:55 +0000 |
| Subject: | note 97572 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168675@lists.php.net to get a copy of this message | ||
I've written a brief, generic function for __get() and __set() that works well implementing
accessor and mutator functions.
This allows the programmer to use implicit accessor and mutator methods when working with attribute
data.
<?php
class MyClass
{
private $degrees
public function __get($name)
{
$fn_name = 'get_' . $name;
if (method_exists($this, $fn_name))
{
return $this->$fn_name();
}
else
{
return null;
}
}
public function __set($name, $value)
{
$fn_name = 'set_' . $name;
if (method_exists($this, $fn_name))
{
$this->$fn_name($value);
}
}
private function get_degrees()
{
return $this->degrees;
}
private function set_degrees($value)
{
$this->degrees = $value % 360;
if ($degrees < 0) $this->degrees += 360;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 129.21.28.8
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/97572
Del: integrated -- https://master.php.net/note/delete/97572/integrated
Del: useless -- https://master.php.net/note/delete/97572/useless
Del: bad code -- https://master.php.net/note/delete/97572/bad+code
Del: spam -- https://master.php.net/note/delete/97572/spam
Del: non-english -- https://master.php.net/note/delete/97572/non-english
Del: in docs -- https://master.php.net/note/delete/97572/in+docs
Del: other reasons-- https://master.php.net/note/delete/97572
Reject -- https://master.php.net/note/reject/97572
Search -- https://master.php.net/manage/user-notes.php