PHP4: Using create_function to create an object method

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello all,

I was playing with create_function the other day and found that it is not
possible to use create_function to create a method for an object. Code example:

<?php
$test = create_function( '', 'echo "Testing";' );
$test(); // will work

$t = 'test';
${$t}(); // will work

$o = new stdClass();
$o->test = create_function( '', 'echo "Testing";' );
$o->test(); // Will break:
            // Fatal error: Call to undefined function:  test()
            // in /www/devel/jmiller/include/test.php on line 10
?>

I saw several work arounds for this in the comments section of the PHP manual,
but I did not see anything in the manual that explicitly states that this will
not work. On an intuitive level, it would seem to me that it would and should. 
Using call_user_func( $o->test ) does work.

I did some google searching but I didn't come up with anything that seemed
relevant. Could someone point me to some documentation that explains why
$o->test() does not work? It would seem to me that this would be a useful
feature for dynamically building objects for use in SOAP, for example, without
having to eval() a big block of code.

>From what I understand, in PHP5 there is a magical __call() method for all
classes/objects, but in PHP4 this capability does not exist.

No, I'm not actually trying to play with this in any serious manner, nor do I
intend to write 'real' code that uses this... Just a curiousity and something
to play around with. (o:

Thank you, all.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux