note 50578 added to ref.classobj
| From: | cjones at osu1 dot php dot net | Date: | Thu, 03 Mar 2005 21:27:53 +0000 |
| Subject: | note 50578 added to ref.classobj | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85890@lists.php.net to get a copy of this message | ||
If anyone is interested in looking for a way to dynamically load existing objects into a class, here
is what I found very useful.
//---------------------------------------------------------
// Dynamically load External Objects into a class
function objRef ( &$obj ) {
eval("\$this->obj_".get_class($obj)." = \$obj;");
}
//---------------------------------------------------------
// Reference by using: $this->obj_[object Name]->[var|f{}]
Example:
class date { function date ( ) { $this->date = "March 3rd"; } }
class time { function time ( ) { $this->time = "12:30pm"; } }
class show {
function objRef ( &$obj ){
eval("\$this->obj_".get_class($obj)." = \$obj;");
}
function test ( $var ){
echo "$var".$this->obj_date->date." @ ".$this->obj_time->time;
}
}
$date = new date;
$time = new time;
$show = new show;
$show->objRef($date);
$show->objRef($time);
$show->test("Time Now => ");
// Output: Time Now => March 3rd @ 12:30pm
I found the prefix 'obj_' before the class name useful because it helped me to
automatically identify external object references when scanning through my scripts. You can omit
this if you want. Hope this helps someone.
----
Manual Page -- http://www.php.net/manual/en/ref.classobj.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50578
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50578&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50578&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50578&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50578&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50578&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50578&report=yes
Search -- http://master.php.net/manage/user-notes.php