note 52877 modified in language.oop5 by et

From: Date: Sat, 15 Oct 2005 09:17:31 +0000
Subject: note 52877 modified in language.oop5 by et
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-97062@lists.php.net to get a copy of this message
[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. --was-- 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. http://php.net/manual/en/language.oop5.php

« previous php.notes (#97062) next »