note 50524 added to ref.classobj
| From: | http://sc dot tri-bit dot com/StoneCypher at osu1 dot php dot net | Date: | Wed, 02 Mar 2005 18:25:44 +0000 |
| Subject: | note 50524 added to ref.classobj | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85810@lists.php.net to get a copy of this message | ||
to covertka at muohio dot edu and pillepop2003 at yahoo dot de:
There's a much easier solution to getting a class' name for working with a factory
function. Let's assume you're doing something like this:
<?php
function FactoryFunction($whatever, $instancedata) {
switch ($whatever) {
case 'stuff' : return new Stuff($instancedata);
case 'otherstuff' : return new Otherstuff($instancedata);
}
}
?>
Now, consider the named parameter idiom and remember that PHP uses hashes for everything; as a
result make the following changes:
<?php
function FactoryFunction($whatever, $instancedata) {
switch ($whatever) {
case 'stuff' : return array('typeis'=>'stuff',
'instance'=>new Stuff($instancedata));
case 'otherstuff' : return array('typeis'=>'otherstuff',
'instance'=>new Otherstuff($instancedata));
}
}
?>
Nice 'n simple. It seems that what the original poster wanted was something like C++ static
data members; unfortunately as PHP4 has no static variables at all, there would need to be
significant language change to support static-like behavior. If you move to PHP5, the static
keyword solves your problem cleanly.
----
Manual Page -- http://www.php.net/manual/en/ref.classobj.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50524
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50524&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50524&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50524&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50524&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50524&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50524&report=yes
Search -- http://master.php.net/manage/user-notes.php