class Foo{private $foo;protected $bar;public $baz;
public function __construct(){$this->foo = 1;$this->bar = 2;$this->baz = new StdClass;}}
var_dump( (array) new Foo );
// Suppose 'result' is the end product from some query $query
$result = $mysqli->query($query);$result = db_result_to_array($result);
function db_result_to_array($result){$res_array = array();
for ($count=0; $row = $result->fetch_assoc(); $count++)$res_array[$count] = $row;
return $res_array;}
function dismount($object) {$reflectionClass = new ReflectionClass(get_class($object));$array = array();foreach ($reflectionClass->getProperties() as $property) {$property->setAccessible(true);$array[$property->getName()] = $property->getValue($object);$property->setAccessible(false);}return $array;}
class Test{const A = 1;public $b = 'two';private $c = test::A;
public function __toArray(){return call_user_func('get_object_vars', $this);}}
$my_test = new Test();var_dump((array)$my_test);var_dump($my_test->__toArray());
function objectToArray($d) {if (is_object($d)) {// Gets the properties of the given object// with get_object_vars function$d = get_object_vars($d);}
if (is_array($d)) {/** Return array converted to object* Using __FUNCTION__ (Magic constant)* for recursive call*/return array_map(__FUNCTION__, $d);} else {// Return arrayreturn $d;}}
另一个将Array转换为stdClass的自定义函数:
function arrayToObject($d) {if (is_array($d)) {/** Return array converted to object* Using __FUNCTION__ (Magic constant)* for recursive call*/return (object) array_map(__FUNCTION__, $d);} else {// Return objectreturn $d;}}
使用示例:
// Create new stdClass Object$init = new stdClass;
// Add some test data$init->foo = "Test data";$init->bar = new stdClass;$init->bar->baaz = "Testing";$init->bar->fooz = new stdClass;$init->bar->fooz->baz = "Testing again";$init->foox = "Just test";
// Convert array to object and then object back to array$array = objectToArray($init);$object = arrayToObject($array);
// Print objects and arrayprint_r($init);echo "\n";print_r($array);echo "\n";print_r($object);
function readObject($object) {$name = get_class ($object);$name = str_replace('\\', "\\\\", $name); // Outcomment this line, if you don't use// class namespaces approach in your project$raw = (array)$object;
$attributes = array();foreach ($raw as $attr => $val) {$attributes[preg_replace('('.$name.'|\*|)', '', $attr)] = $val;}return $attributes;}
class PersonArray implements \ArrayAccess, \IteratorAggregate{public function __construct(Person $person) {$this->person = $person;}// ...}
如果您需要所有属性,请使用传输对象:
class PersonTransferObject{private $person;
public function __construct(Person $person) {$this->person = $person;}
public function toArray() {return [// 'name' => $this->person->getName();];}
}
class Car {/** @var int */private $color;
/** @var string */private $model;
/** @var string */private $type;
/*** @return int*/public function getColor(): int{return $this->color;}
/*** @param int $color* @return Car*/public function setColor(int $color): Car{$this->color = $color;return $this;}
/*** @return string*/public function getModel(): string{return $this->model;}
/*** @param string $model* @return Car*/public function setModel(string $model): Car{$this->model = $model;
return $this;}
/*** @return string*/public function getType(): string{return $this->type;}
/*** @param string $type* @return Car*/public function setType(string $type): Car{$this->type = $type;
return $this;}}
装饰器
class CarArrayDecorator{/** @var Car */private $car;
/*** CarArrayDecorator constructor.* @param Car $car*/public function __construct(Car $car){$this->car = $car;}
/*** @return array*/public function getArray(): array{return ['color' => $this->car->getColor(),'type' => $this->car->getType(),'model' => $this->car->getModel(),];}}
用法
$car = new Car();$car->setType('type#');$car->setModel('model#1');$car->setColor(255);
$carDecorator = new CarArrayDecorator($car);$carResponseData = $carDecorator->getArray();
/*** This method returns the array corresponding to an object, including non public members.** If the deep flag is true, is will operate recursively, otherwise (if false) just at the first level.** @param object $obj* @param bool $deep = true* @return array* @throws \Exception*/public static function objectToArray(object $obj, bool $deep = true){$reflectionClass = new \ReflectionClass(get_class($obj));$array = [];foreach ($reflectionClass->getProperties() as $property) {$property->setAccessible(true);$val = $property->getValue($obj);if (true === $deep && is_object($val)) {$val = self::objectToArray($val);}$array[$property->getName()] = $val;$property->setAccessible(false);}return $array;}
使用示例,以下代码:
class AA{public $bb = null;protected $one = 11;
}
class BB{protected $two = 22;}
$a = new AA();$b = new BB();$a->bb = $b;
var_dump($a)