note 101288 added to language.types.object
| From: | Cosmitar:mhherrera31athotmail at osu1 dot php dot net | Date: | Wed, 08 Dec 2010 06:47:40 +0000 |
| Subject: | note 101288 added to language.types.object | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174697@lists.php.net to get a copy of this message | ||
i would like to share a curious behavior on casted objects. Casting an object from a class with
private/protected attributes results a stdClass with a private/protected attribute for get.
Example:
<?PHP
class Foo{
private $priv = 1;
public $pub = 2;
public function getSimple(){
return (object)(array) $this; //the array cast is to force a stdClass result
}
}
$bar = new Foo();
var_dump($bar->getSimple();// output: object(stdClass)#3 (2) { ["priv:private"]=>
int(1) ["pub"]=> int(2) }
var_dump($bar->getSimple()->priv);// output: NULL, not a Fatal Error
var_dump($bar->getSimple()->pub);// output: int(2)
$barSimple = $bar->getSimple();
$barSimple->priv = 10;
var_dump($barSimple->priv);// output: int(10)
?>
----
Server IP: 69.147.83.197
Probable Submitter: 190.245.206.34
----
Manual Page -- http://www.php.net/manual/en/language.types.object.php
Edit -- https://master.php.net/note/edit/101288
Del: integrated -- https://master.php.net/note/delete/101288/integrated
Del: useless -- https://master.php.net/note/delete/101288/useless
Del: bad code -- https://master.php.net/note/delete/101288/bad+code
Del: spam -- https://master.php.net/note/delete/101288/spam
Del: non-english -- https://master.php.net/note/delete/101288/non-english
Del: in docs -- https://master.php.net/note/delete/101288/in+docs
Del: other reasons-- https://master.php.net/note/delete/101288
Reject -- https://master.php.net/note/reject/101288
Search -- https://master.php.net/manage/user-notes.php