- Php foreach (get_object_vars($value_object) as $key => $value) This tutorials show you how to use get_object_vars. get_object_vars is used in the following way. foreach (get_object_vars($value_object) as $key => $value) The get_object_vars is declared as follows: get_object_vars( object $object): array The return value is Returns an associative array of defined object accessible non-static properties for the specified object in scope. Examples /* w w w . d e m o 2 s . c o m * / /** * Description of ExtendableObject * * @author Pablo Pacheco */ if (!class _exists('OriggamiExtendableObject' )) < class OriggamiExtendableObject < //put your code here protected $_value_object; public static function factory($value_object = NULL) < $class = get_called_class (); $newObject = new $class ($value_object); foreach (get_object_vars($value_object) as $key => $value) $newObject->$key = $value; > return $newObject; > > > Related demo2s.com | Email: | Demo Source and Support. All rights reserved. Источник get_object_vars Gets the accessible non-static properties of the given object according to scope. Parameters Return Values Returns an associative array of defined object accessible non-static properties for the specified object in scope. Examples Example #1 Use of get_object_vars() class foo private $a ; public $b = 1 ; public $c ; private $d ; static $e ; public function test () var_dump ( get_object_vars ( $this )); > > $test = new foo ; var_dump ( get_object_vars ( $test )); The above example will output: array(2) < ["b"]=>int(1) ["c"]=> NULL > array(4) < ["a"]=>NULL ["b"]=> int(1) ["c"]=> NULL ["d"]=> NULL > Note: Uninitialized properties are considered inaccessible, and thus will not be included in the array. See Also User Contributed Notes 5 notes You can still cast the object to an array to get all its members and see its visibility. Example: class Potatoe public $skin ; protected $meat ; private $roots ; function __construct ( $s , $m , $r ) $this -> skin = $s ; $this -> meat = $m ; $this -> roots = $r ; > > $Obj = new Potatoe ( 1 , 2 , 3 ); echo «\n» ; echo «Using get_object_vars:\n» ; $vars = get_object_vars ( $Obj ); print_r ( $vars ); $Arr = (array) $Obj ; print_r ( $Arr ); Using get_object_vars: Array ( [skin] => 1 ) Using array cast: Array ( [skin] => 1 [ * meat] => 2 [ Potatoe roots] => 3 ) As you can see, you can obtain the visibility for each member from this cast. That which seems to be spaces into array keys are ‘
’ characters, so the general rule to parse keys seems to be: Public members: member_name Protected memebers:
*
member_name Private members:
Class_name
member_name I’ve wroten a obj2array function that creates entries without visibility for each key, so you can handle them into the array as it were within the object: function obj2array ( & $Instance ) $clone = (array) $Instance ; $rtn = array (); $rtn [ ‘___SOURCE_KEYS_’ ] = $clone ; while ( list ( $key , $value ) = each ( $clone ) ) $aux = explode ( «
» , $key ); $newkey = $aux [ count ( $aux )- 1 ]; $rtn [ $newkey ] = & $rtn [ ‘___SOURCE_KEYS_’ ][ $key ]; > ?> I’ve created also a bless function that works similar to Perl’s bless, so you can further recast the array converting it in an object of an specific class: // First get source keys if available if ( isset ( $Instance [ ‘___SOURCE_KEYS_’ ])) $Instance = $Instance [ ‘___SOURCE_KEYS_’ ]; > // Get serialization data from array $serdata = serialize ( $Instance ); list ( $array_params , $array_elems ) = explode ( ‘ list ( $array_tag , $array_count ) = explode ( ‘:’ , $array_params , 3 ); $serdata = «O:» . strlen ( $Class ). «:\» $Class \»: $array_count : $Instance = unserialize ( $serdata ); return $Instance ; > ?> With these ones you can do things like: define ( «SFCMS_DIR» , dirname ( __FILE__ ). «/..» ); require_once ( SFCMS_DIR . «/Misc/bless.php» ); class Potatoe public $skin ; protected $meat ; private $roots ; function __construct ( $s , $m , $r ) $this -> skin = $s ; $this -> meat = $m ; $this -> roots = $r ; > function PrintAll () echo «skin keyword»>. $this -> skin . «\n» ; echo «meat keyword»>. $this -> meat . «\n» ; echo «roots keyword»>. $this -> roots . «\n» ; > > $Obj = new Potatoe ( 1 , 2 , 3 ); echo «\n» ; echo «Using get_object_vars:\n» ; $vars = get_object_vars ( $Obj ); print_r ( $vars ); echo «\n\nUsing obj2array func:\n» ; $Arr = obj2array ( $Obj ); print_r ( $Arr ); echo «\n\nSetting all members to 0.\n» ; $Arr [ ‘skin’ ]= 0 ; $Arr [ ‘meat’ ]= 0 ; $Arr [ ‘roots’ ]= 0 ; echo «Converting the array into an instance of the original class.\n» ; bless ( $Arr , Potatoe ); if ( is_object ( $Arr ) ) echo «\$Arr is now an object.\n» ; if ( $Arr instanceof Potatoe ) echo «\$Arr is an instance of Potatoe class.\n» ; > > Please be aware of hidden behaviors with uninitialised properties. The note explains : « Uninitialized properties are considered inaccessible, and thus will not be included in the array. » but that’s not entirely true in PHP 8.1. It depends if the property is type-hinted or not. class Example public $untyped ; public string $typedButNotInitialized ; public ? string $typedOrNullNotInitialized ; public ? string $typedOrNullWithDefaultNull = null ; > var_dump ( get_object_vars (new Example ())); ?> will print : array(2) [«untyped»]=> NULL [«typedOrNullWithDefaultNull»]=> NULL > As you can see, only «untyped» and «typedOrNullWithDefaultNull» properties are dumped with get_object_vars(). You may encounter problems when migrating old source code and adds carelessly types everywhere without proper initialisation (or default) and assuming it defaults to NULL like old code does. You can use an anonymous class to return public variables from inside the class: public function getPublicVars () $me = new class function getPublicVars($object) return get_object_vars($object); > >; return $me->getPublicVars($this); > class Test protected $protected; public $public; private $private; public function getAllVars () return call_user_func(‘get_object_vars’, $this); > public function getPublicVars () $me = new class function getPublicVars($object) return get_object_vars($object); > >; return $me->getPublicVars($this); > > $test = new Test(); print_r(get_object_vars($test)); // array(«public» => NULL) print_r($test->getAllVars()); // array(«protected» => NULL, «public» => NULL, «private» => NULL) print_r($test->getPublicVars()); // array(«public» => NULL) When dealing with a very large quantity of objects, it is worth noting that using `get_object_vars()` may drastically increase memory usage. If instantiated objects only use predefined properties from a class then PHP can use a single hashtable for the class properties, and small memory-efficient arrays for the object properties: If a class is defined with three properties ($foo, $bar, and $baz), «PHP no longer has to store the data in a hashtable, but instead can say that $foo is proprety 0, $bar is proprety 1, $baz is property 2 and then just store the properties in a three-element C array. This means that PHP only needs one hashtable in the class that does the property-name to offset mapping and uses a memory-efficient C-array in the individual objects.» However, if you call `get_object_vars()` on an object like this, then PHP WILL build a hashtable for the individual object. If you have a large quantity of objects, and you call `get_object_vars()` on all of them, then a hashtable will be built for each object, resulting in a lot more memory usage. This can be seen in this bug report: https://bugs.php.net/bug.php?id=79392 The effects of this can be seen in this example: class Example public $foo ; public $bar ; public $baz ; > function printMem ( $label ) $usage = memory_get_usage (); echo sprintf ( ‘%s: %d (%.2f MB)’ , $label , $usage , $usage / 1000000 ) . PHP_EOL ; > $objects = []; for ( $i = 0 ; $i < 20000 ; $i ++) $obj = new Example ; $obj -> foo = bin2hex ( random_bytes ( 5 )); $obj -> bar = bin2hex ( random_bytes ( 5 )); $obj -> baz = bin2hex ( random_bytes ( 5 )); $objects [] = $obj ; > printMem ( ‘before get_object_vars’ ); // Clone each object, and get the vars on the clone foreach ( $objects as $obj ) $c = clone $obj ; $vars = get_object_vars ( $c ); // Accessing and modifying the original object is fine. foreach ( $vars as $var => $val ) $obj -> < $var >= strrev ( $val ); > > printMem ( ‘get_object_vars using clone’ ); // Get the vars on each object directly foreach ( $objects as $obj ) $vars = get_object_vars ( $obj ); // The memory is used even if you do not modify the object. > printMem ( ‘get_object_vars direct access’ ); ?> The output of this is: start: 405704 (0.41 MB) before get_object_vars: 6512416 (6.51 MB) get_object_vars using clone: 6033408 (6.03 MB) get_object_vars direct access: 13553408 (13.55 MB) In short, if you are using classes to avoid additional memory usage associated with hashtables (like in associative arrays), be aware that `get_object_vars()` will create a hashtable for any object passed to it. This appears to be present in all versions of PHP; I’ve tested it on PHP 5, 7, and 8. Quotes are from Nikic’s blog posts on arrays and hashtable memory usage, and Github gist «Why objects (usually) use less memory than arrays in PHP». It seems like there’s no function that determines all the *static* variables of a class. I’ve come out with this one as I needed it in a project: function get_class_static_vars ( $object ) < return array_diff ( get_class_vars ( get_class ( $object )), get_object_vars ( $object )); > ?> It relies on an interesting property: the fact that get_object_vars only returns the non-static variables of an object. Classes/Object Functions class_alias class_exists enum_exists get_called_class get_class_methods get_class_vars get_class get_declared_classes get_declared_interfaces get_declared_traits get_mangled_object_vars get_object_vars get_parent_class interface_exists is_a is_subclass_of method_exists property_exists trait_exists __autoload Источник Php foreach (get_object_vars($this) as $k => $v) This tutorials show you how to use get_object_vars. get_object_vars is used in the following way. foreach (get_object_vars($this) as $k => $v) The get_object_vars is declared as follows: get_object_vars( object $object): array The return value is Returns an associative array of defined object accessible non-static properties for the specified object in scope. Examples // w w w . d e m o 2 s . c o m namespace ManaPHP\Configure < use ManaPHP\Component; use ManaPHP\Di; /** * Class Configure * * @package ManaPHP */ class Configure extends Component implements ConfigureInterface < public $debug = true; public function __debugInfo() < $data = []; foreach (get_object_vars($this) as $k => $v) if (is_scalar($v) || is_array ($v) || $v instanceof \stdClass) < $data[$k] = $v; >> return $data; > > > Related demo2s.com | Email: | Demo Source and Support. All rights reserved. Источник Php foreach (get_object_vars($this) as $key => $values) This tutorials show you how to use get_object_vars. get_object_vars is used in the following way. foreach (get_object_vars($this) as $key => $values) The get_object_vars is declared as follows: get_object_vars( object $object): array The return value is Returns an associative array of defined object accessible non-static properties for the specified object in scope. Examples /* w w w . d e m o 2 s . c o m * / namespace JVBase\Model; abstract class AbstractModel < public function __construct(AbstractModel $model = null) < if ($model) < foreach (get_object_vars($this) as $key => $values) $getter = 'get' . ucfirst($key); $setter = 'set' . ucfirst($key); if (method_exists($this, $setter) && method_exists($this, $getter)) < $this->$setter($model->$getter()); > > > return $this; > public function has($property) < $getter = 'get' . ucfirst($property); if (method_exists($this, $getter)) < if ('s' === substr($property, 0, -1) && is_array ($this->$getter())) < return true; >elseif ($this->$getter()) < return true; >> > public function __toString() < return get_class ($this); > > Related demo2s.com | Email: | Demo Source and Support. All rights reserved. Источник
- Examples
- Related
- get_object_vars
- Parameters
- Return Values
- Examples
- See Also
- User Contributed Notes 5 notes
- Php foreach (get_object_vars($this) as $k => $v) This tutorials show you how to use get_object_vars. get_object_vars is used in the following way. foreach (get_object_vars($this) as $k => $v) The get_object_vars is declared as follows: get_object_vars( object $object): array The return value is Returns an associative array of defined object accessible non-static properties for the specified object in scope. Examples // w w w . d e m o 2 s . c o m namespace ManaPHP\Configure < use ManaPHP\Component; use ManaPHP\Di; /** * Class Configure * * @package ManaPHP */ class Configure extends Component implements ConfigureInterface < public $debug = true; public function __debugInfo() < $data = []; foreach (get_object_vars($this) as $k => $v) if (is_scalar($v) || is_array ($v) || $v instanceof \stdClass) < $data[$k] = $v; >> return $data; > > > Related demo2s.com | Email: | Demo Source and Support. All rights reserved. Источник Php foreach (get_object_vars($this) as $key => $values) This tutorials show you how to use get_object_vars. get_object_vars is used in the following way. foreach (get_object_vars($this) as $key => $values) The get_object_vars is declared as follows: get_object_vars( object $object): array The return value is Returns an associative array of defined object accessible non-static properties for the specified object in scope. Examples /* w w w . d e m o 2 s . c o m * / namespace JVBase\Model; abstract class AbstractModel < public function __construct(AbstractModel $model = null) < if ($model) < foreach (get_object_vars($this) as $key => $values) $getter = 'get' . ucfirst($key); $setter = 'set' . ucfirst($key); if (method_exists($this, $setter) && method_exists($this, $getter)) < $this->$setter($model->$getter()); > > > return $this; > public function has($property) < $getter = 'get' . ucfirst($property); if (method_exists($this, $getter)) < if ('s' === substr($property, 0, -1) && is_array ($this->$getter())) < return true; >elseif ($this->$getter()) < return true; >> > public function __toString() < return get_class ($this); > > Related demo2s.com | Email: | Demo Source and Support. All rights reserved. Источник
- Examples
- Related
- Php foreach (get_object_vars($this) as $key => $values) This tutorials show you how to use get_object_vars. get_object_vars is used in the following way. foreach (get_object_vars($this) as $key => $values) The get_object_vars is declared as follows: get_object_vars( object $object): array The return value is Returns an associative array of defined object accessible non-static properties for the specified object in scope. Examples /* w w w . d e m o 2 s . c o m * / namespace JVBase\Model; abstract class AbstractModel < public function __construct(AbstractModel $model = null) < if ($model) < foreach (get_object_vars($this) as $key => $values) $getter = 'get' . ucfirst($key); $setter = 'set' . ucfirst($key); if (method_exists($this, $setter) && method_exists($this, $getter)) < $this->$setter($model->$getter()); > > > return $this; > public function has($property) < $getter = 'get' . ucfirst($property); if (method_exists($this, $getter)) < if ('s' === substr($property, 0, -1) && is_array ($this->$getter())) < return true; >elseif ($this->$getter()) < return true; >> > public function __toString() < return get_class ($this); > > Related demo2s.com | Email: | Demo Source and Support. All rights reserved. Источник
- Examples
- Related
Php foreach (get_object_vars($value_object) as $key => $value)
This tutorials show you how to use get_object_vars.
get_object_vars is used in the following way.
foreach (get_object_vars($value_object) as $key => $value)
The get_object_vars is declared as follows:
get_object_vars( object $object): array
The return value is Returns an associative array of defined object accessible non-static properties for the specified object in scope.
Examples
/*w w w . d e mo 2 s. c o m */ /** * Description of ExtendableObject * * @author Pablo Pacheco */ if (!class_exists('OriggamiExtendableObject')) < class OriggamiExtendableObject < //put your code here protected $_value_object; public static function factory($value_object = NULL) < $class = get_called_class(); $newObject = new $class($value_object); foreach (get_object_vars($value_object) as $key => $value) $newObject->$key = $value; > return $newObject; > > >
Related
demo2s.com | Email: | Demo Source and Support. All rights reserved.
get_object_vars
Gets the accessible non-static properties of the given object according to scope.
Parameters
Return Values
Returns an associative array of defined object accessible non-static properties for the specified object in scope.
Examples
Example #1 Use of get_object_vars()
class foo private $a ;
public $b = 1 ;
public $c ;
private $d ;
static $e ;
public function test () var_dump ( get_object_vars ( $this ));
>
>
$test = new foo ;
var_dump ( get_object_vars ( $test ));
The above example will output:
array(2) < ["b"]=>int(1) ["c"]=> NULL > array(4) < ["a"]=>NULL ["b"]=> int(1) ["c"]=> NULL ["d"]=> NULL >
Note:
Uninitialized properties are considered inaccessible, and thus will not be included in the array.
See Also
User Contributed Notes 5 notes
You can still cast the object to an array to get all its members and see its visibility. Example:
class Potatoe public $skin ;
protected $meat ;
private $roots ;
function __construct ( $s , $m , $r ) $this -> skin = $s ;
$this -> meat = $m ;
$this -> roots = $r ;
>
>
$Obj = new Potatoe ( 1 , 2 , 3 );
echo «\n» ;
echo «Using get_object_vars:\n» ;
$vars = get_object_vars ( $Obj );
print_r ( $vars );
$Arr = (array) $Obj ;
print_r ( $Arr );
Using get_object_vars:
Array
(
[skin] => 1
)
Using array cast:
Array
(
[skin] => 1
[ * meat] => 2
[ Potatoe roots] => 3
)
As you can see, you can obtain the visibility for each member from this cast. That which seems to be spaces into array keys are ‘\0’ characters, so the general rule to parse keys seems to be:
Public members: member_name
Protected memebers: \0*\0member_name
Private members: \0Class_name\0member_name
I’ve wroten a obj2array function that creates entries without visibility for each key, so you can handle them into the array as it were within the object:
function obj2array ( & $Instance ) $clone = (array) $Instance ;
$rtn = array ();
$rtn [ ‘___SOURCE_KEYS_’ ] = $clone ;
while ( list ( $key , $value ) = each ( $clone ) ) $aux = explode ( «\0» , $key );
$newkey = $aux [ count ( $aux )- 1 ];
$rtn [ $newkey ] = & $rtn [ ‘___SOURCE_KEYS_’ ][ $key ];
>
?>
I’ve created also a bless function that works similar to Perl’s bless, so you can further recast the array converting it in an object of an specific class:
// First get source keys if available
if ( isset ( $Instance [ ‘___SOURCE_KEYS_’ ])) $Instance = $Instance [ ‘___SOURCE_KEYS_’ ];
>
// Get serialization data from array
$serdata = serialize ( $Instance );
list ( $array_params , $array_elems ) = explode ( ‘ list ( $array_tag , $array_count ) = explode ( ‘:’ , $array_params , 3 );
$serdata = «O:» . strlen ( $Class ). «:\» $Class \»: $array_count :
$Instance = unserialize ( $serdata );
return $Instance ;
>
?>
With these ones you can do things like:
define ( «SFCMS_DIR» , dirname ( __FILE__ ). «/..» );
require_once ( SFCMS_DIR . «/Misc/bless.php» );
class Potatoe public $skin ;
protected $meat ;
private $roots ;
function __construct ( $s , $m , $r ) $this -> skin = $s ;
$this -> meat = $m ;
$this -> roots = $r ;
>
function PrintAll () echo «skin keyword»>. $this -> skin . «\n» ;
echo «meat keyword»>. $this -> meat . «\n» ;
echo «roots keyword»>. $this -> roots . «\n» ;
>
>
$Obj = new Potatoe ( 1 , 2 , 3 );
echo «\n» ;
echo «Using get_object_vars:\n» ;
$vars = get_object_vars ( $Obj );
print_r ( $vars );
echo «\n\nUsing obj2array func:\n» ;
$Arr = obj2array ( $Obj );
print_r ( $Arr );
echo «\n\nSetting all members to 0.\n» ;
$Arr [ ‘skin’ ]= 0 ;
$Arr [ ‘meat’ ]= 0 ;
$Arr [ ‘roots’ ]= 0 ;
echo «Converting the array into an instance of the original class.\n» ;
bless ( $Arr , Potatoe );
if ( is_object ( $Arr ) ) echo «\$Arr is now an object.\n» ;
if ( $Arr instanceof Potatoe ) echo «\$Arr is an instance of Potatoe class.\n» ;
>
>
Please be aware of hidden behaviors with uninitialised properties. The note explains : « Uninitialized properties are considered inaccessible, and thus will not be included in the array. » but that’s not entirely true in PHP 8.1. It depends if the property is type-hinted or not.
class Example
public $untyped ;
public string $typedButNotInitialized ;
public ? string $typedOrNullNotInitialized ;
public ? string $typedOrNullWithDefaultNull = null ;
>
var_dump ( get_object_vars (new Example ()));
?>
will print :
array(2) [«untyped»]=>
NULL
[«typedOrNullWithDefaultNull»]=>
NULL
>
As you can see, only «untyped» and «typedOrNullWithDefaultNull» properties are dumped with get_object_vars(). You may encounter problems when migrating old source code and adds carelessly types everywhere without proper initialisation (or default) and assuming it defaults to NULL like old code does.
You can use an anonymous class to return public variables from inside the class:
public function getPublicVars () $me = new class function getPublicVars($object) return get_object_vars($object);
>
>;
return $me->getPublicVars($this);
>
class Test protected $protected;
public $public;
private $private;
public function getAllVars () return call_user_func(‘get_object_vars’, $this);
>
public function getPublicVars () $me = new class function getPublicVars($object) return get_object_vars($object);
>
>;
return $me->getPublicVars($this);
>
>
$test = new Test();
print_r(get_object_vars($test)); // array(«public» => NULL)
print_r($test->getAllVars()); // array(«protected» => NULL, «public» => NULL, «private» => NULL)
print_r($test->getPublicVars()); // array(«public» => NULL)
When dealing with a very large quantity of objects, it is worth noting that using `get_object_vars()` may drastically increase memory usage.
If instantiated objects only use predefined properties from a class then PHP can use a single hashtable for the class properties, and small memory-efficient arrays for the object properties:
If a class is defined with three properties ($foo, $bar, and $baz), «PHP no longer has to store the data in a hashtable, but instead can say that $foo is proprety 0, $bar is proprety 1, $baz is property 2 and then just store the properties in a three-element C array. This means that PHP only needs one hashtable in the class that does the property-name to offset mapping and uses a memory-efficient C-array in the individual objects.»
However, if you call `get_object_vars()` on an object like this, then PHP WILL build a hashtable for the individual object. If you have a large quantity of objects, and you call `get_object_vars()` on all of them, then a hashtable will be built for each object, resulting in a lot more memory usage. This can be seen in this bug report: https://bugs.php.net/bug.php?id=79392
The effects of this can be seen in this example:
class Example public $foo ;
public $bar ;
public $baz ;
>
function printMem ( $label ) $usage = memory_get_usage ();
echo sprintf ( ‘%s: %d (%.2f MB)’ , $label , $usage , $usage / 1000000 ) . PHP_EOL ;
>
$objects = [];
for ( $i = 0 ; $i < 20000 ; $i ++) $obj = new Example ;
$obj -> foo = bin2hex ( random_bytes ( 5 ));
$obj -> bar = bin2hex ( random_bytes ( 5 ));
$obj -> baz = bin2hex ( random_bytes ( 5 ));
$objects [] = $obj ;
>
printMem ( ‘before get_object_vars’ );
// Clone each object, and get the vars on the clone
foreach ( $objects as $obj ) $c = clone $obj ;
$vars = get_object_vars ( $c );
// Accessing and modifying the original object is fine.
foreach ( $vars as $var => $val ) $obj -> < $var >= strrev ( $val );
>
>
printMem ( ‘get_object_vars using clone’ );
// Get the vars on each object directly
foreach ( $objects as $obj ) $vars = get_object_vars ( $obj );
// The memory is used even if you do not modify the object.
>
printMem ( ‘get_object_vars direct access’ );
?>
The output of this is:
start: 405704 (0.41 MB)
before get_object_vars: 6512416 (6.51 MB)
get_object_vars using clone: 6033408 (6.03 MB)
get_object_vars direct access: 13553408 (13.55 MB)
In short, if you are using classes to avoid additional memory usage associated with hashtables (like in associative arrays), be aware that `get_object_vars()` will create a hashtable for any object passed to it.
This appears to be present in all versions of PHP; I’ve tested it on PHP 5, 7, and 8.
Quotes are from Nikic’s blog posts on arrays and hashtable memory usage, and Github gist «Why objects (usually) use less memory than arrays in PHP».
It seems like there’s no function that determines all the *static* variables of a class.
I’ve come out with this one as I needed it in a project:
function get_class_static_vars ( $object ) <
return array_diff ( get_class_vars ( get_class ( $object )), get_object_vars ( $object ));
>
?>
It relies on an interesting property: the fact that get_object_vars only returns the non-static variables of an object.
- Classes/Object Functions
- class_alias
- class_exists
- enum_exists
- get_called_class
- get_class_methods
- get_class_vars
- get_class
- get_declared_classes
- get_declared_interfaces
- get_declared_traits
- get_mangled_object_vars
- get_object_vars
- get_parent_class
- interface_exists
- is_a
- is_subclass_of
- method_exists
- property_exists
- trait_exists
- __autoload
Php foreach (get_object_vars($this) as $k => $v)
This tutorials show you how to use get_object_vars.
get_object_vars is used in the following way.
foreach (get_object_vars($this) as $k => $v)
The get_object_vars is declared as follows:
get_object_vars( object $object): array
The return value is Returns an associative array of defined object accessible non-static properties for the specified object in scope.
Examples
// ww w . d e m o 2 s. co m namespace ManaPHP\Configure < use ManaPHP\Component; use ManaPHP\Di; /** * Class Configure * * @package ManaPHP */ class Configure extends Component implements ConfigureInterface < public $debug = true; public function __debugInfo() < $data = []; foreach (get_object_vars($this) as $k => $v) if (is_scalar($v) || is_array($v) || $v instanceof \stdClass) < $data[$k] = $v; >> return $data; > > >
Related
demo2s.com | Email: | Demo Source and Support. All rights reserved.
Php foreach (get_object_vars($this) as $key => $values)
This tutorials show you how to use get_object_vars.
get_object_vars is used in the following way.
foreach (get_object_vars($this) as $key => $values)
The get_object_vars is declared as follows:
get_object_vars( object $object): array
The return value is Returns an associative array of defined object accessible non-static properties for the specified object in scope.
Examples
/*w w w . d e m o 2 s .c o m*/ namespace JVBase\Model; abstract class AbstractModel < public function __construct(AbstractModel $model = null) < if ($model) < foreach (get_object_vars($this) as $key => $values) $getter = 'get' . ucfirst($key); $setter = 'set' . ucfirst($key); if (method_exists($this, $setter) && method_exists($this, $getter)) < $this->$setter($model->$getter()); > > > return $this; > public function has($property) < $getter = 'get' . ucfirst($property); if (method_exists($this, $getter)) < if ('s' === substr($property, 0, -1) && is_array($this->$getter())) < return true; >elseif ($this->$getter()) < return true; >> > public function __toString() < return get_class($this); > >
Related
demo2s.com | Email: | Demo Source and Support. All rights reserved.