note 85260 added to function.get-object-vars
| From: | michal dot kocarek at brainbox dot cz | Date: | Fri, 22 Aug 2008 10:44:59 +0000 |
| Subject: | note 85260 added to function.get-object-vars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143742@lists.php.net to get a copy of this message | ||
When casting object instance to an array, PHP creates array keys for public, and also protected and
private members. So this can be solution how to view public/private/protected properties.
Examples:
- protected property named "_wheelCount" defined in class "Car" can be found in
array under key "\x00*\x00_wheelCount"
- private property named "_dbConnection" defined in class "SqlDb" can be found
in array under key "\x00SqlDb\x00_dbConnection";
(note that \x00 is real NUL-character, so it cannot be viewed on console nor browser).
<?
class Foo {
public $public;
protected $protected;
private $private;
public function __construct() {
$this->public = 'p';
$this->public = 'i am public';
$this->protected = 'i am protected';
$this->private = 'i am private';
}
}
$foo = new Foo();
$foo_to_array = (array)$foo; // Cast object to array
var_dump($foo_to_array);
/* Outputs following: (note that char \x00 is normally not visible, I substitied it for keeping
example clear )
array(3) {
["public"]=>
string(11) "i am public"
["\x00*\x00protected"]=>
string(14) "i am protected"
["\x00Foo\x00private"]=>
string(12) "i am private"
}
*/
?>
----
Server IP: 147.32.160.17
Probable Submitter: 193.179.134.86
----
Manual Page -- http://www.php.net/manual/en/function.get-object-vars.php
Edit -- https://master.php.net/note/edit/85260
Del: integrated -- https://master.php.net/note/delete/85260/integrated
Del: useless -- https://master.php.net/note/delete/85260/useless
Del: bad code -- https://master.php.net/note/delete/85260/bad+code
Del: spam -- https://master.php.net/note/delete/85260/spam
Del: non-english -- https://master.php.net/note/delete/85260/non-english
Del: in docs -- https://master.php.net/note/delete/85260/in+docs
Del: other reasons-- https://master.php.net/note/delete/85260
Reject -- https://master.php.net/note/reject/85260
Search -- https://master.php.net/manage/user-notes.php