note 34954 added to ref.overload
| From: | Justin at rack1 dot php dot net | Date: | Wed, 13 Aug 2003 08:16:02 +0000 |
| Subject: | note 34954 added to ref.overload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-54059@lists.php.net to get a copy of this message | ||
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 ...
*/
----
Manual Page -- http://www.php.net/manual/en/ref.overload.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34954
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34954&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34954&report=yes