views:

1146

answers:

4

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

is it posible or is there any generic work around?

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. –

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

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.

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.

A: 

Sounds like you want to be using inheritance.

Ignacio Vazquez-Abrams
no, no inheritance. Every class already have a parent class. They just have a set of equal named attributes and reperent the same thing. try to change the inheritance tree would be a total mess.
Important little details like this are something that you really ought to include in the question.
Ignacio Vazquez-Abrams
+2  A: 

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:

rockacola
+1  A: 

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

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;
}

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

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

+1 I have used this in the past to convert Cart to SavedCart in a MySQL user session table.
hopeseekr
A: 

You do not need casting. Everything is dynamic.

I have a class Discount.
I have several classes that extends this class:
ProductDiscount
StoreDiscount
ShippingDiscount
...

Somewhere in the code I have:

$pd = new ProductDiscount();
$pd->setDiscount(5, ProductDiscount::PRODUCT_DISCOUNT_PERCENT);
$pd->setProductId(1);

$this->discounts[] = $pd;

.....

$sd = new StoreDiscount();
$sd->setDiscount(5, StoreDiscount::STORE_DISCOUNT_PERCENT);
$sd->setStoreId(1);

$this->discounts[] = $sd;

And somewhere I have:

foreach ($this->discounts as $discount){

    if ($discount->getDiscountType()==Discount::DISCOUNT_TYPE_PRODUCT){

        $productDiscount = $discount; // you do not need casting.
        $amount = $productDiscount->getDiscountAmount($this->getItemTotalPrice());
        ...
    }

}// foreach

Where getDiscountAmount is ProductDiscount specific function, and getDiscountType is Discount specific function.

darko petreski