note 52877 added to language.oop5
| From: | AJG at osu1 dot php dot net | Date: | Mon, 16 May 2005 15:13:44 +0000 |
| Subject: | note 52877 added to language.oop5 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89494@lists.php.net to get a copy of this message | ||
I never found a way to create a new, empty, generic object so I coded something up myself. In
addition, let me show you some imperfect examples.
<?
// Incorrect:
$obj1 = (object) '';
$obj2 = (object) 0;
$obj3 = (object) 1;
$obj4 = (object) true;
$obj5 = (object) false;
// Syntax error:
$obj6 = (object);
// Correct:
$obj7 = (object) NULL;
?>
The first five ones will work, but will create a 'scalar' field in the object. This is
undesirable at best, and can be a real nuisance when doing object automation. For that reason, I now
use the following function, which produces a 'clean' object. It's also better
semantically, in my opinion.
<?
function object() { return ((object) NULL); }
// Usage:
$object = object();
// Just like:
$array = array();
?>
Please post if you see any problems arising due to this method; thanks. Otherwise enjoy.
--AJG.
----
Manual Page -- http://www.php.net/manual/en/language.oop5.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52877
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52877&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52877&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52877&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52877&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52877&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52877&report=yes
Search -- http://master.php.net/manage/user-notes.php