Re: [PHP] PHP4: Using create_function to create an object method

2004-12-23 Thread Greg Donald
On Thu, 23 Dec 2004 15:57:08 -0600, [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote:
 but I did not see anything in the manual that explicitly states that this will
 not work.

create_function -- Create an anonymous (lambda-style) function

The keyword here is anonymous, as in 'not named'.  The anonymous
function you create will have a name but it's not a name you can use,
it's just a randomly generated string for preventing naming
collisions.  That's why you must access anonymous functions with a
variable.


-- 
Greg Donald
Zend Certified Engineer
http://gdconsultants.com/
http://destiney.com/

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



[PHP] PHP4: Using create_function to create an object method

2004-12-23 Thread kermodebear
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