note 85260 deleted from function.get-object-vars by tularis
| From: | tularis@php.net | Date: | Fri, 22 Aug 2008 15:08:11 +0000 |
| Subject: | note 85260 deleted from function.get-object-vars by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143749@lists.php.net to get a copy of this message | ||
Note Submitter: michal dot kocarek at brainbox dot cz
Reason: in docs
----
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"
}
*/
?>