MemoryRefresh!

Features / Class reflection   Class reflection



Questions Answers 0%

Pages   0 from 91
Questions   0 from 142

Reset


Get private property
<?php class Foo { public $foo = 1; protected $bar = 2; private $baz = 3; } $foo = new Foo(); // echo $foo->baz; // --- Look Here --- // // Fatal error: Cannot access private property $reflector = new ReflectionObject($foo); $property = $reflector->getProperty('baz'); $property->setAccessible(true); echo $property->getValue($foo); // --- Look Here --- // // display 3 Get private properties
<?php class Foo { public $foo = 1; protected $bar = 2; private $baz = 3; } $foo = new Foo(); $reflector = new ReflectionObject($foo); $properties = $reflector->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED); array_map(function(&$x) { $x->setAccessible(true); }, $properties); var_dump($properties); /* array (size=2) 0 => object(ReflectionProperty)[3] public 'name' => string 'bar' (length=3) public 'class' => string 'Foo' (length=3) 1 => object(ReflectionProperty)[4] public 'name' => string 'baz' (length=3) public 'class' => string 'Foo' (length=3) */ Get parent properties
<?php class Ford { private $model; protected $foo; public $bar; } class Car extends Ford { private $year; } $class = new ReflectionClass('Car'); var_dump($class->getProperties()); // First chunk of output var_dump($class->getParentClass()->getProperties()); // Second chunk php.net stackoverflow.com

Related Pages

Introduction
Autoload
Class reflection
Magic methods
Late static binding
Type hinting
Exceptions


0% 100%
 
 
0 pages 91 pages
1) Create a reflexion for class foo {}




2) Get private property outside the class




3) Set reflection class property to public







Get private property
<?php class Foo { public $foo = 1; protected $bar = 2; private $baz = 3; } $foo = new Foo(); // echo $foo->baz; // --- Look Here --- // // Fatal error: Cannot access private property $reflector = new ReflectionObject($foo); $property = $reflector->getProperty('baz'); $property->setAccessible(true); echo $property->getValue($foo); // --- Look Here --- // // display 3 Get private properties
<?php class Foo { public $foo = 1; protected $bar = 2; private $baz = 3; } $foo = new Foo(); $reflector = new ReflectionObject($foo); $properties = $reflector->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED); array_map(function(&$x) { $x->setAccessible(true); }, $properties); var_dump($properties); /* array (size=2) 0 => object(ReflectionProperty)[3] public 'name' => string 'bar' (length=3) public 'class' => string 'Foo' (length=3) 1 => object(ReflectionProperty)[4] public 'name' => string 'baz' (length=3) public 'class' => string 'Foo' (length=3) */ Get parent properties
<?php class Ford { private $model; protected $foo; public $bar; } class Car extends Ford { private $year; } $class = new ReflectionClass('Car'); var_dump($class->getProperties()); // First chunk of output var_dump($class->getParentClass()->getProperties()); // Second chunk php.net stackoverflow.com

References









Programming

Php
   
Regex
   
MySQL
   
Css
   
jQuery
   
Git
   


References