How to Cast Objects in PHP

后端 未结 12 1966
轮回少年
轮回少年 2020-11-27 04:24

Ive some classes that share some attributes, and i would like to do something like:

$dog = (Dog) $cat;

is it possible or is there any gener

12条回答
  •  青春惊慌失措
    2020-11-27 04:48

    Without using inheritance (as mentioned by author), it seems like you are looking for a solution that can transform one class to another with preassumption of the developer knows and understand the similarity of 2 classes.

    There's no existing solution for transforming between objects. What you can try out are:

    • get_object_vars() : convert object to array
    • Cast to Object: convert array to object

提交回复
热议问题