Re: Object to array conversion oddity

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 2006-09-23 at 22:35 +0100, Marcus Bointon wrote:
> I've written a function that does a conversion that matches the docs,  
> based on the other info I've mentioned:
> 
> /**
> * Clean up the name munging that PHP applies while casting from  
> object to array
> * The resulting array looks like what the documentation says that  
> (array)$object delivers, but actually doesn't
> * @param object $object The object to convert
> * @return array
> */
> function object2array($object) {
> 	$class = get_class($object);
> 	$array = (array)$object;
> 	$propArray = array();
> 	$matches = array();
> 	foreach ($array as $key => $value) {
> 		if (preg_match('/^\0(([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\0 
> ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$/', $key, $matches)) {
> 			//It's a private property
> 			if ($matches[1] == $class) { //Ignore private props of superclasses
> 				$propArray[$matches[2]] = $value;
> 			}
> 		} elseif (preg_match('/^\0(\*)\0([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f- 
> \xff]*)$/', $key, $matches)) {
> 			//It's a protected property
> 			$propArray[$matches[2]] = $value;
> 		} else {
> 			//It's a public property
> 			$propArray[$key] = $value;
> 		}
> 	}
> 	return $propArray;
> }
> 
> Works nicely for me.

It's broken, it doesn't preserve references.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux