note 92123 added to language.oop5.basic

From: Date: Thu, 09 Jul 2009 22:26:10 +0000
Subject: note 92123 added to language.oop5.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-158125@lists.php.net to get a copy of this message
stdClass is the default PHP object. stdClass has no properties, methods or parent. It does not support magic methods, and implements no interfaces. When you cast a scalar or array as Object, you get an instance of stdClass. You can use stdClass whenever you need a generic object instance. <?php // ways of creating stdClass instances $x = new stdClass; $y = (object) null; // same as above $z = (object) 'a'; // creates property 'scalar' = 'a' $a = (object) array('property1' => 1, 'property2' => 'b'); ?> stdClass is NOT a base class! PHP classes do not automatically inherit from any class. All classes are standalone, unless they explicitly extend another class. PHP differs from many object-oriented languages in this respect. <?php // CTest does not derive from stdClass class CTest { public $property1; } $t = new CTest; var_dump($t instanceof stdClass); // false var_dump(is_subclass_of($t, 'stdClass')); // false echo get_class($t) . "\n"; // 'CTest' echo get_parent_class($t) . "\n"; // false (no parent) ?> You cannot define a class named 'stdClass' in your code. That name is already used by the system. You can define a class named 'Object'. You could define a class that extends stdClass, but you would get no benefit, as stdClass does nothing. (tested on PHP 5.2.8) ---- Server IP: 69.147.83.197 Probable Submitter: 76.231.221.254 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php Edit -- https://master.php.net/note/edit/92123 Del: integrated -- https://master.php.net/note/delete/92123/integrated Del: useless -- https://master.php.net/note/delete/92123/useless Del: bad code -- https://master.php.net/note/delete/92123/bad+code Del: spam -- https://master.php.net/note/delete/92123/spam Del: non-english -- https://master.php.net/note/delete/92123/non-english Del: in docs -- https://master.php.net/note/delete/92123/in+docs Del: other reasons-- https://master.php.net/note/delete/92123 Reject -- https://master.php.net/note/reject/92123 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158125) next »