Re: PHP5: __call() implementation EXAMPLE
| From: | Vivian Steller | Date: | Thu, 05 Feb 2004 15:34:50 +0000 |
| Subject: | Re: PHP5: __call() implementation EXAMPLE | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-176895@lists.php.net to get a copy of this message | ||
Vivian Steller wrote:
> Hello,
>
> as you know there is a new callback function __call($method, $params) in
> php5. the __call() method of an object is called, if the method named
> $method is not declared in this class.
>
> i want this function simply call the method of another class with
> "SomeClass::$method(...)" but i'm getting into trouble (bit heavy
> programming!:) passing the arguments (stored as Array in $params) to the
> "SomeClass::$method([arguments])" method...
> further, i think i'm getting problems with objects passed through the
> __call() method?!
>
> any suggestions?
> thanks
>
> vivi
ok, here is an example: hope you'll understand what i want...
<pre>
<?php
class MyClass {
function __call($method, $params) {
// $params = Array(mixed var, mixed var, ...);
print("request for " . $method . "()\n");
// how to get objects in this string?!
// do i have to implement "switch($type) { case "object": ...
}" here?!
$parameterstring = "\"" . $params[0] . "\"";
eval("OtherClass::" . $method . "(" . $parameterstring .
");");
}
}
class OtherClass {
function OtherMethod($obj = false) {
if(is_object($obj)) {
// this method receives an object to show the difficulties with
object
passed through call...
if(method_exists($obj, "printSomething")) {
$obj->printSomething();
}
}
else {
print("Error: no object passed! '" . $obj .
"'\n");
}
}
}
class Whatever {
function printSomething() {
print("passing object through __call() is fine\n");
}
}
$someObj = new Whatever();
// $someObj->printSomething();
$MyObj = new MyClass();
// this works perfectly
$MyObj->OtherMethod("passing a string");
// this is my problem...
$MyObj->OtherMethod($someObj);
?>
</pre>
please, try and help :)
vivi