phpオブジェクトを配列に変換
3385 ワード
function objectToArray($object)
{
$reflect = new ReflectionClass($object);
$pros = $reflect->getDefaultProperties();
$e = array();
foreach($pros as $k=>$v)
{
if( is_object($object->$k))
{
//$e[$k]=$object->$k;
$e[$k]= objectToArray($object->$k);
}
else {
$e[$k]=$object->$k;
}
}
return $e;
}
php , php , 、 、 、 , 。 API。 API, , , 。(ps: )
API php oop , 、 , , , , 。 oop 。
ReflectionClass
[php]
<?php
class ReflectionClass implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//
public string getName()
//
public bool isInternal()
//
public bool isUserDefined()
//
public bool isInstantiable()
//
public bool hasConstant(string name)
//
public bool hasMethod(string name)
//
public bool hasProperty(string name)
//
public string getFileName()
// ,
public int getStartLine()
//
public int getEndLine()
//
public string getDocComment()
//
public ReflectionMethod getConstructor()
//
public ReflectionMethod getMethod(string name)
//
public ReflectionMethod[] getMethods()
//
public ReflectionProperty getProperty(string name)
//
public ReflectionProperty[] getProperties()
//
public array getConstants()
//
public mixed getConstant(string name)
//
public ReflectionClass[] getInterfaces()
//
public bool isInterface()
//
public bool isAbstract()
//
public bool isFinal()
// final
public int getModifiers()
// ,
// Reflection::getModifierNames($class->getModifiers())
public bool isInstance(stdclass object)
//
public stdclass newInstance(mixed* args)
//
public ReflectionClass getParentClass()
//
public bool isSubclassOf(ReflectionClass class)
//
public array getStaticProperties()
//
public mixed getStaticPropertyValue(string name [, mixed default])
// , private,
public void setStaticPropertyValue(string name, mixed value)
// , private, ,
public array getDefaultProperties()
// ,
public bool isIterateable()
public bool implementsInterface(string name)
//
public ReflectionExtension getExtension()
public string getExtensionName()
}
?>
:
[php]
class MoveDataFactory
{
/**
* Description: , mode
* @return
*/
public function GetMoveClass($classname)
{
$reflectionclass = new ReflectionClass($classname);
return $reflectionclass->newInstance();
}
}