如何在PHP中投射对象 [英] How to Cast Objects in PHP

查看:106
本文介绍了如何在PHP中投射对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些共享某些属性的clases,我想做一些事情:
$ dog =(Dog)$ cat;

Ive some clases that share some attributes, and i would like to do something like: $dog = (Dog) $cat;

posible还是有什么类似的工作吗?

is it posible or is there any generic work around?

它不是一个超类,或者一个接口或任何方式的相关。他们只是2不同的clases我想php映射属性从一个猫类到狗,并给我新的对象。 -

Its not a superclass, or a interface or related in any way. They are just 2 different clases i would like php map the attributes from a cat class to a dog and give me the new object. –

我猜ihave指定更多的原因似乎是一个毫无意义的事情。

i guess ihave to specify a little bit more cause seem like a senseless thing to do.

clases从不同的父类继承,导致ive基于保存方法创建一个继承树,也许我的坏从开始,但问题是,我有很多clases几乎相等,但是与mysql和另一个xml文件。所以我有:
类MySql_SomeEntity扩展SomeMysqlInteract {}
和Xml_SomeEntity扩展SomeXmlInteract {}
它有点更深的树,但问题。我不能让他们继承自同一个类,因为多重继承不是下降的,我不能分离当前与超级行为的交互会导致很大的麻烦。

ive clases that inherits from diferents parent clases cause ive made an inheritance tree based on the saving method, maybe my bad from the begining, but the problem is that i have a lot of clases that are practically equal but interacts one with mysql and the otherone with xml files. so i have: class MySql_SomeEntity extends SomeMysqlInteract{} and Xml_SomeEntity extends SomeXmlInteract{} its a little bit deeper tree but the problem its that. i cant make them inherits from the same class cause multimple inheritance is not alowed, and i cant separate current interaction with superclases cause would be a big throuble.

基本上每个都是实用的。

Basically the atributes in each one are practical the same.

因为我有很多这种加工clases我想做一些通用铸造或类似的东西,可以转换(传递值每个赞美),但我试图搜索最简单的方式给每个人这个clases。

since i have a lot of this maching clases i would like to do some generic casting or something like it that can converts (pass the values to each atribute) and but im trying to search the simplest way to everyone of this clases.

推荐答案

PHP中没有类型转换用户定义对象的内置方法。也就是说,这里有几个可能的解决方案:

There is no built-in method for type casting of user defined objects in PHP. That said, here are several possible solutions:

1)使用类似下面的函数来反序列化对象,改变字符串,使你需要的属性包括在

1) Use a function like the one below to deserialize the object, alter the string so that the properties you need are included in the new object once it's deserialized.

function cast($obj, $to_class) {
  if(class_exists($to_class)) {
    $obj_in = serialize($obj);
    $obj_out = 'O:' . strlen($to_class) . ':"' . $to_class . '":' . substr($obj_in, $obj_in[2] + 7);
    return unserialize($obj_out);
  }
  else
    return false;
}


$ b <他们全部或使用get_object_vars()。

2) Alternatively, you could copy the object's properties using reflection / manually iterating through them all or using get_object_vars().

这篇文章应该启发你对PHP的黑暗角落,并在用户级实现typecasting。

This article should enlighten you on the "dark corners of PHP" and implementing typecasting on the user level.

这篇关于如何在PHP中投射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆