Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class aClass
- {
- protected $protected_property = 'protected_value';
- private $private_property = 'private_value';
- public $public_property = 'public_value';
- public function getAllProperties()
- {
- return get_object_vars($this);
- }
- }
- $an_object = new aClass;
- //------------------------------------------------------------------------------
- $start = microtime(true);
- $an_array = array();
- for ($i = 0; $i <= 10000; $i++)
- {
- $reflection = new \ReflectionClass($an_object);
- $properties = $reflection->getProperties();
- foreach ($properties as $property)
- {
- $property->setAccessible(true);
- $an_array[$property->getName()] = $property->getValue($an_object);
- if (!$property->isPublic())
- $property->setAccessible(false);
- }
- $reflection = null;
- $properties = null;
- }
- echo microtime(true) - $start, PHP_EOL; // 0.171475172043
- var_dump($an_array);
- //------------------------------------------------------------------------------
- $start = microtime(true);
- $an_array = array();
- for ($i = 0; $i <= 10000; $i++)
- {
- reset($an_object);
- while (list($key, $val) = each($an_object))
- {
- $key = ($key{0} === "\0") ? substr($key, strpos($key, "\0", 1) + 1) : $key;
- $an_array[$key] = $val;
- }
- }
- echo microtime(true) - $start, PHP_EOL; // 0.0651688575745
- var_dump($an_array);
- //------------------------------------------------------------------------------
- $start = microtime(true);
- $an_array = array();
- for ($i = 0; $i <= 10000; $i++)
- {
- $an_array = $an_object->getAllProperties();
- }
- echo microtime(true) - $start, PHP_EOL; // 0.0214040279388
- var_dump($an_array);
- //------------------------------------------------------------------------------
- $an_array = array(
- 'protected_property' => 'other_protected_value',
- 'private_property' => 'other_private_value',
- 'public_property' => 'other_public_value',
- );
- //------------------------------------------------------------------------------
- $start = microtime(true);
- for ($i = 0; $i <= 10000; $i++)
- {
- $reflection = new \ReflectionClass($an_object);
- $properties = $reflection->getProperties();
- foreach ($properties as $property)
- {
- $property->setAccessible(true);
- $property->setValue($an_object, $an_array[$property->getName()]);
- if (!$property->isPublic())
- $property->setAccessible(false);
- }
- $reflection = null;
- $properties = null;
- }
- echo microtime(true) - $start, PHP_EOL; // 0.166314125061
- var_dump($an_object);
- //------------------------------------------------------------------------------
- $start = microtime(true);
- for ($i = 0; $i <= 10000; $i++)
- {
- array_walk($an_object, function(&$val, $key, $array)
- {
- $key = ($key{0} === "\0") ? substr($key, strpos($key, "\0", 1) + 1) : $key;
- $val = $array[$key];
- }, $an_array);
- }
- echo microtime(true) - $start, PHP_EOL; // 0.0659220218658
- var_dump($an_object);
- //------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement