note 52877 deleted from language.oop5 by colder
| From: | colder@php.net | Date: | Sun, 19 Aug 2007 23:32:58 +0000 |
| Subject: | note 52877 deleted from language.oop5 by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-129622@lists.php.net to get a copy of this message | ||
Note Submitter: AJG
----
[Note from et@php.net:
Your way to create a new, empty, generic object is:
<?php $obj = new stdClass(); ?>
]
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.