将stdClass对象转换/转换为另一个类

问题描述 投票:75回答:8

我正在使用第三方存储系统,它只返回我的stdClass对象,无论我输入什么因为一些不明原因。所以我很想知道是否有办法将stdClass对象转换/转换为给定类型的完整对象。

例如,有些东西:

//$stdClass is an stdClass instance
$converted = (BusinessClass) $stdClass;

我只是将stdClass转换为数组并将其提供给BusinessClass构造函数,但也许有一种方法可以恢复我不知道的初始类。

注意:我对“更改您的存储系统”类型的答案不感兴趣,因为它不是您感兴趣的点。请将其视为关于语言能力的学术问题。

干杯

php stdclass
8个回答
82
投票

有关可能的演员阵容,请参阅manual on Type Juggling

允许的演员阵容是:

  • (int),(integer) - 强制转换为整数
  • (bool),(boolean) - 强制转换为布尔值
  • (浮动),(双),(真实) - 施放到浮动
  • (字符串) - 强制转换为字符串
  • (数组) - 强制转换为数组
  • (对象) - 强制转换为对象
  • (未设置) - 强制转换为NULL(PHP 5)

你必须使用write a Mapper来执行从stdClass到另一个具体类的转换。不应该太难做。

或者,如果您处于一种骇人的心情,您可以调整以下代码:

function arrayToObject(array $array, $className) {
    return unserialize(sprintf(
        'O:%d:"%s"%s',
        strlen($className),
        $className,
        strstr(serialize($array), ':')
    ));
}

伪造一个数组到某个类的对象。这通过首先序列化数组然后更改序列化数据以使其代表某个类来工作。然后,结果将反序列化为此类的实例。但就像我说的那样,它是黑客,所以期待副作用。

对于对象,代码将是

function objectToObject($instance, $className) {
    return unserialize(sprintf(
        'O:%d:"%s"%s',
        strlen($className),
        $className,
        strstr(strstr(serialize($instance), '"'), ':')
    ));
}

49
投票

你可以使用上面的函数来编译不相似的类对象(PHP> = 5.3)

/**
 * Class casting
 *
 * @param string|object $destination
 * @param object $sourceObject
 * @return object
 */
function cast($destination, $sourceObject)
{
    if (is_string($destination)) {
        $destination = new $destination();
    }
    $sourceReflection = new ReflectionObject($sourceObject);
    $destinationReflection = new ReflectionObject($destination);
    $sourceProperties = $sourceReflection->getProperties();
    foreach ($sourceProperties as $sourceProperty) {
        $sourceProperty->setAccessible(true);
        $name = $sourceProperty->getName();
        $value = $sourceProperty->getValue($sourceObject);
        if ($destinationReflection->hasProperty($name)) {
            $propDest = $destinationReflection->getProperty($name);
            $propDest->setAccessible(true);
            $propDest->setValue($destination,$value);
        } else {
            $destination->$name = $value;
        }
    }
    return $destination;
}

例:

class A 
{
  private $_x;   
}

class B 
{
  public $_x;   
}

$a = new A();
$b = new B();

$x = cast('A',$b);
$x = cast('B',$a);

13
投票

要将stdClass的所有现有属性移动到指定类名的新对象:

/**
 * recast stdClass object to an object with type
 *
 * @param string $className
 * @param stdClass $object
 * @throws InvalidArgumentException
 * @return mixed new, typed object
 */
function recast($className, stdClass &$object)
{
    if (!class_exists($className))
        throw new InvalidArgumentException(sprintf('Inexistant class %s.', $className));

    $new = new $className();

    foreach($object as $property => &$value)
    {
        $new->$property = &$value;
        unset($object->$property);
    }
    unset($value);
    $object = (unset) $object;
    return $new;
}

用法:

$array = array('h','n');

$obj=new stdClass;
$obj->action='auth';
$obj->params= &$array;
$obj->authKey=md5('i');

class RestQuery{
    public $action;
    public $params=array();
    public $authKey='';
}

$restQuery = recast('RestQuery', $obj);

var_dump($restQuery, $obj);

输出:

object(RestQuery)#2 (3) {
  ["action"]=>
  string(4) "auth"
  ["params"]=>
  &array(2) {
    [0]=>
    string(1) "h"
    [1]=>
    string(1) "n"
  }
  ["authKey"]=>
  string(32) "865c0c0b4ab0e063e5caa3387c1a8741"
}
NULL

这是有限的因为new运算符,因为它不知道它将需要哪些参数。对于你的情况可能适合。


10
投票

我有一个非常类似的问题。简化的反射解决方案对我来说很好用:

public static function cast($destination, \stdClass $source)
{
    $sourceReflection = new \ReflectionObject($source);
    $sourceProperties = $sourceReflection->getProperties();
    foreach ($sourceProperties as $sourceProperty) {
        $name = $sourceProperty->getName();
        $destination->{$name} = $source->$name;
    }
    return $destination;
}

8
投票

希望有人觉得这很有用

// new instance of stdClass Object
$item = (object) array(
    'id'     => 1,
    'value'  => 'test object',
);

// cast the stdClass Object to another type by passing
// the value through constructor
$casted = new ModelFoo($item);

// OR..

// cast the stdObject using the method
$casted = new ModelFoo;
$casted->cast($item);
class Castable
{
    public function __construct($object = null)
    {
        $this->cast($object);
    }

    public function cast($object)
    {
        if (is_array($object) || is_object($object)) {
            foreach ($object as $key => $value) {
                $this->$key = $value;
            }
        }
    }
} 
class ModelFoo extends Castable
{
    public $id;
    public $value;
}

5
投票

更改深度转换功能(使用递归)

/**
 * Translates type
 * @param $destination Object destination
 * @param stdClass $source Source
 */
private static function Cast(&$destination, stdClass $source)
{
    $sourceReflection = new \ReflectionObject($source);
    $sourceProperties = $sourceReflection->getProperties();
    foreach ($sourceProperties as $sourceProperty) {
        $name = $sourceProperty->getName();
        if (gettype($destination->{$name}) == "object") {
            self::Cast($destination->{$name}, $source->$name);
        } else {
            $destination->{$name} = $source->$name;
        }
    }
}

0
投票

BTW:如果序列化,转换非常重要,主要是因为反序列化会破坏对象的类型并转换为stdclass,包括DateTime对象。

我更新了@Jadrovski的例子,现在它允许对象和数组。

$stdobj=new StdClass();
$stdobj->field=20;
$obj=new SomeClass();
fixCast($obj,$stdobj);

示例数组

$stdobjArr=array(new StdClass(),new StdClass());
$obj=array(); 
$obj[0]=new SomeClass(); // at least the first object should indicates the right class.
fixCast($obj,$stdobj);

代码:(递归)。但是,我不知道它是否与数组递归。可能是它缺少额外的is_array

public static function fixCast(&$destination,$source)
{
    if (is_array($source)) {
        $getClass=get_class($destination[0]);
        $array=array();
        foreach($source as $sourceItem) {
            $obj = new $getClass();
            fixCast($obj,$sourceItem);
            $array[]=$obj;
        }
        $destination=$array;
    } else {
        $sourceReflection = new \ReflectionObject($source);
        $sourceProperties = $sourceReflection->getProperties();
        foreach ($sourceProperties as $sourceProperty) {
            $name = $sourceProperty->getName();
            if (is_object(@$destination->{$name})) {
                fixCast($destination->{$name}, $source->$name);
            } else {
                $destination->{$name} = $source->$name;
            }
        }
    }
}

0
投票

考虑向Business Class添加新方法:

public static function fromStdClass(\stdClass $in): BusinessClass
{
  $out                   = new self();
  $reflection_object     = new \ReflectionObject($in);
  $reflection_properties = $reflection_object->getProperties();
  foreach ($reflection_properties as $reflection_property)
  {
    $name = $reflection_property->getName();
    if (property_exists('BusinessClass', $name))
    {
      $out->{$name} = $in->$name;
    }
  }
  return $out;
}

然后你可以从$ stdClass创建一个新的BusinessClass:

$converted = BusinessClass::fromStdClass($stdClass);
© www.soinside.com 2019 - 2024. All rights reserved.