note 32488 added to keyword.paamayim-nekudotayim
| From: | gk at proliberty dot com | Date: | Wed, 28 May 2003 23:33:23 +0000 |
| Subject: | note 32488 added to keyword.paamayim-nekudotayim | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-49217@lists.php.net to get a copy of this message | ||
Contrary to the comment above, I have found that call_user_func()
is inferior to eval() because call_user_func() does not correctly
update objects passed by reference.
In the example below, each of my subclasses calls init(), passing
a reference to the current object, whose class variables are
initialized by classes in the hierarchy. The class variable
$this->classFiles contains the paths of each file for classes in
the hierarchy. It is not updated correctly using call_user_func()
but eval() works fine.
<?php
///////////////////////////////////////////////////////
/*
xobj( );
constructor
SUBCLASSES MUST NOT have any constructors
*/
/////////////////////////////////////////////////////////
function xobj( $params= NULL ){
$current_class=get_class($this);
$this->init($this, $current_class, $params);
// $this->classFiles[0] is the file containing the final subclass
$this->classFile=$this->classFiles[0];
print_r($this->classFiles); exit;
$this->_init($params);
}
///////////////////////////////////////////////////////
/*
void init(&$this_class, $current_class, $params= NULL );
initialization
this class MUST be overridden to set $this->classFiles correctly:
initialization requires __FILE__ to get the path of every class file
*/
/////////////////////////////////////////////////////////
function init(&$this_class, $current_class, $params= NULL ){
$this_class->classFiles[] = __FILE__;
$parent_class=get_parent_class($current_class);
if( !empty($parent_class)){
eval("$parent_class::init(\$this_class, \$parent_class, \$params);");
// call_user_func() DOES NOT CORRECTLY UPDATE $this_class->classFiles:
//call_user_func(array($parent_class,'init'),
$this_class,$parent_class,$params);
}
} // init()
?>
RESULT, using eval():
Array
(
[0] => /usr/local/apache/htdocs/common/php/xobj/xobj_subclass.php
[1] => /usr/local/apache/htdocs/common/php/xobj/xobj.php
)
RESULT, using call_user_func():
Array
(
[0] => /usr/local/apache/htdocs/common/php/xobj/xobj_subclass.php
)
----
Manual Page -- http://www.php.net/manual/en/keyword.paamayim-nekudotayim.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32488
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32488&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32488&report=yes