note 102880 modified in language.oop5.properties by danbrown
| From: | danbrown@php.net | Date: | Sat, 12 Mar 2011 14:11:05 +0000 |
| Subject: | note 102880 modified in language.oop5.properties by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177820@lists.php.net to get a copy of this message | ||
$this can be cast to array. But when doing so, it prefixes the property names/new array keys with
certain data depending on the property classification. Public property names are not changed.
Protected properties are prefixed with a space-padded '*'. Private properties are
prefixed with the space-padded class name...
<?php
class test
{
public $var1 = 1;
protected $var2 = 2;
private $var3 = 3;
static $var4 = 4;
public function toArray()
{
return (array) $this;
}
}
$t = new test;
print_r($t->toArray());
/* outputs:
Array
(
[var1] => 1
[ * var2] => 2
[ test var3] => 3
)
*/
?>
This is documented behavior when converting any object to an array (see
</language.types.array.php#language.types.array.casting> PHP manual page). All properties
regardless of visibility will be shown when casting an object to array (with exceptions of a few
built-in objects).
To get an array with all property names unaltered, use the 'get_object_vars($this)'
function in any method within class scope to retrieve an array of all properties regardless of
external visibility, or 'get_object_vars($object)' outside class scope to retrieve an
array of only public properties (see: </function.get-object-vars.php> PHP manual page).
--was--
$this can be cast to array. But when doing so, it prefixes the property names/new array keys with
certain data depending on the property classification. Public property names are not changed.
Protected properties are prefixed with a space-padded '*'. Private properties are
prefixed with the space-padded class name...
<?php
class test
{
public $var1 = 1;
protected $var2 = 2;
private $var3 = 3;
static $var4 = 4;
public function toArray()
{
return (array) $this;
}
}
$t = new test;
print_r($t->toArray());
/* outputs:
Array
(
[var1] => 1
[ * var2] => 2
[ test var3] => 3
)
*/
?>
http://php.net/manual/en/language.oop5.properties.php