note 118679 added to language.types.object
| From: | Anthony | Date: | Mon, 18 Jan 2016 22:36:47 +0000 |
| Subject: | note 118679 added to language.types.object | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204712@lists.php.net to get a copy of this message | ||
In PHP 7 there are a few ways to create an empty object:
<?php
$obj1 = new \stdClass; // Instantiate stdClass object
$obj2 = new class{}; // Instantiate anonymous class
$obj3 = (object)[]; // Cast empty array to object
var_dump($obj1); // object(stdClass)#1 (0) {}
var_dump($obj2); // object(class@anonymous)#2 (0) {}
var_dump($obj3); // object(stdClass)#3 (0) {}
?>
$obj1 and $obj3 are the same type, but $obj1 !== $obj3. Also, all three will json_encode() to a
simple JS object {}:
<?php
echo json_encode([
new \stdClass,
new class{},
(object)[],
]);
?>
Outputs: [{},{},{}]
----
Server IP: 72.52.91.14
Probable Submitter: 86.29.95.83
----
Manual Page -- http://php.net/manual/en/language.types.object.php
Edit -- https://master.php.net/note/edit/118679
Del: integrated -- https://master.php.net/note/delete/118679/integrated
Del: useless -- https://master.php.net/note/delete/118679/useless
Del: bad code -- https://master.php.net/note/delete/118679/bad+code
Del: spam -- https://master.php.net/note/delete/118679/spam
Del: non-english -- https://master.php.net/note/delete/118679/non-english
Del: in docs -- https://master.php.net/note/delete/118679/in+docs
Del: other reasons-- https://master.php.net/note/delete/118679
Reject -- https://master.php.net/note/reject/118679
Search -- https://master.php.net/manage/user-notes.php