[PHP] Re: PHP5: __call() implementation EXAMPLE
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: PHP5: __call() implementation EXAMPLE
From: Vivian Steller [EMAIL PROTECTED] 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 . );); Can't you just say: OtherClass::$method($params); here? } } 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 ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: PHP5: __call() implementation EXAMPLE
Vivian Steller wrote: ... 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 $parameterstring = \ . $params[0] . \; eval(OtherClass:: . $method . ( . $parameterstring . );); } } ... ok, i'm sorry, but i found the solution for my own: on the top you see some snippet of my EXAMPLE Message before. you can implement the $parameterstring generation algorithm like this (and its very very easy!!) $parameterstring = ; for($i = 0; $i count($params); $i++) { if(!empty($parameterstring)) { $parameterstring .= , ; } $parameterstring .= \$params[ . $i . ]; } replace the line $parameterstring = \ . $params[0] . \; with this code snippet and it works... any way, thanks. vivi -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: PHP5: __call() implementation EXAMPLE
John W. Holmes wrote: From: Vivian Steller [EMAIL PROTECTED] Vivian Steller wrote: ... 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 . );); Can't you just say: OtherClass::$method($params); here? ... ---John Holmes... No, sorry that doesn't work. Interpreter is trying to acces static member $method .. and this member does not exist. The error message: Fatal error: Access to undeclared static property: OtherClass::$method in ... Thanks for your time. vivi -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php