note 34954 deleted from ref.overload by sobak
| From: | sobak@php.net | Date: | Tue, 22 Nov 2016 03:48:46 +0000 |
| Subject: | note 34954 deleted from ref.overload by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207442@lists.php.net to get a copy of this message | ||
Note Submitter: Justin B
----
Some useful things to know about overloading:
__call($m,$p,&$r) returns $r back to you, not whatever you put after the keyword return. What
you return determines whether or not the parser consideres the function defined.
__get($var,&$val) returns $val back to you, so fill up $val with what you want then return true
or false, same as above.
when extending classes, you must overload the most extended level class for it to work:
class TestClass
{
var $x = "x";
var $y = "y";
var $z = "z";
function __call($method,$params,&$return)
{
$return = "Hello, you called $method with
".var_export($params,true)."<br>\n";
return true;
}
function __get($var,&$val)
{
if($var == "l") { $val = $this->x; return true; }
return false;
}
}
overload('TestClass');
$test = new TestClass;
print $test->hello();
print $test->goodbye();
print $test->x;
print $test->l;
print $test->n;
class Test2 extends TestClass
{
}
$test2 = new Test2;
print $test2->hello();
/* output:
Hello, you called hello with array()
Hello, you called goodbye with array()
xx
Fatal Error: Call to undefined function hello() in ...
*/