note 97572 deleted from language.oop5.overloading by joey
| From: | joey@php.net | Date: | Mon, 31 Oct 2011 09:39:37 +0000 |
| Subject: | note 97572 deleted from language.oop5.overloading by joey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184240@lists.php.net to get a copy of this message | ||
Note Submitter: zzzzbov
----
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;
}
}
?>