note 74492 modified in function.create-function by danbrown

From: Date: Sat, 19 Jul 2008 21:19:33 +0000
Subject: note 74492 modified in function.create-function by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-141753@lists.php.net to get a copy of this message
In the process of migrating a PHP4 codebase to PHP5, I ran into a peculiar problem. In the library, every class was derived from a generic class called 'class_container'. 'class_container' contained an array called runtime_functions and a method called class_function that was as follows: <?php function class_function($name,$params,$code) { $this->runtime_functions[$name] = create_function($params,$code); } ?> In a subclass of class_container, there was a function that utilized class_function() to store some custom lambda functions that were self-referential: <?php function myfunc($name,$code) { $this->class_function($name,'$theobj','$this=&$theobj;'.$code); } ?> In PHP4, this worked just fine. The idea was to write blocks of code at the subclass level, such as "echo $this->id;", then simply $MYOBJ->myfunc("go","echo $this->id;"); and later call it like $MYOBJ->runtime_functions["go"](); It essentially worked exactly like binding anonymous functions to objects in Javascript. Note how the "$this" keyword had to be manually redefined for the $code block to work. In PHP5, however, you can't redeclare $this without getting a fatal error, so the code had to be updated to: <?php function myfunc($name,$code) { $this->class_function($name,'$this',$code); } ?> Apparently create_function() allows you to set $this via a function argument, allowing you to bind anonymous functions to instantiated objects. Thought it might be useful to somebody. --was-- In the process of migrating a PHP4 codebase to PHP5, I ran into a peculiar problem. In the library, every class was derived from a generic class called 'class_container'. 'class_container' contained an array called runtime_functions and a method called class_function that was as follows: function class_function($name,$params,$code) { $this->runtime_functions[$name] = create_function($params,$code); } In a subclass of class_container, there was a function that utilized class_function() to store some custom lambda functions that were self-referential: function myfunc($name,$code) { $this->class_function($name,'$theobj','$this=&$theobj;'.$code); } In PHP4, this worked just fine. The idea was to write blocks of code at the subclass level, such as "echo $this->id;", then simply $MYOBJ->myfunc("go","echo $this->id;"); and later call it like $MYOBJ->runtime_functions["go"](); It essentially worked exactly like binding anonymous functions to objects in Javascript. Note how the "$this" keyword had to be manually redefined for the $code block to work. In PHP5, however, you can't redeclare $this without getting a fatal error, so the code had to be updated to: function myfunc($name,$code) { $this->class_function($name,'$this',$code); } Apparently create_function() allows you to set $this via a function argument, allowing you to bind anonymous functions to instantiated objects. Thought it might be useful to somebody. http://php.net/manual/en/function.create-function.php

« previous php.notes (#141753) next »