views:

66

answers:

5

What would be a good way (along with any pros and cons) of initializing an instance of a PHP class with another object of the same class (ideally in PHP 4.x)?

Here in initialize() is essentially what I'd like to be able to do (example is extremely simplified from my use-case, see below):

$product = new Product('Widget');
$product2 = new Product('Widget #2');
$product->initialize($product2);
echo $product->name;  // echos "Widget #2"

class Product {
 var $name;
 function __constructor($name) {
   $this->name = $name;
 }
 function initialize($product) {
   // I know this cannot be done this way in PHP. 
   // What are the alternatives and their pros & cons?
   $this = $product;  
 }
}

I know this may not be "good programming practice"; with 20+ years programming experience on other languages I know a bit about what's good and what's not. So hopefully we won't get hung up on if doing this makes sense or not. I have a use-case working with some open-source code that I can't change so please just bear with me on my need for it. I'm actually trying to create an OOP wrapper around some really ugly array code buried deep in the core of WordPress.

I'm trying to write it so in future versions they can move away from the ugly array-based code because everyone will be using the new API that otherwise fully encapsulated these nasty arrays. But to make it work elegantly I need to be able to do the above (in PHP 4.x) and I don't want to write code that just copies the properties.

Thanks in advance for your help.

UPDATE

Many of you are suggesting clone but unless I misunderstand that doesn't address the question. clone makes a copy; that's not the crux of the question. I'm instead trying to get the constructed object to "become" the object passed in. At this point I'm assuming there isn't a way to do that based on the fact that 0 out of 5 answers have suggested anything but I'll wait a bit longer before selecting a best in case it was simply that my questions was unclear.

A: 

maybe

$product = new Product('Widget');
$product2 = new Product(null, $product);
echo $product2->name;  // echos "Widget #2"

class Product {
 var $name;
 function __constructor($name, $product = null) {
   $this->name = !empty($name) ? $name : $product->name;
 }
}
Ascherer
*@Ascherer* Thanks for the response. Are you suggesting that if I have save 15 properties I need 15 of the assignment lines? That's what I'm already doing and trying to do it more maintainably.
MikeSchinkel
ive always had a protected $_data variable thats just an array with the variables i want. But yeah, not saying this is the best path to go, just one that works
Ascherer
+1  A: 
class Product {
    var $name;
    function __construct($value) {
        if (is_a($value, 'Product')) {
            $this->name = $value->name;
        } else {
            $this->name = $value;
        }
    }
}

Similarly, you can use instanceof instead of is_a if you prefer (depending on your PHP version).

Now you can pass a Product instance OR a name to the construct.

$product = new Product('Something');
$clone = new Product($product);
Matt Huggins
*@Matt Huggins* Thanks for the response. Same reply as to *@Ascherer*: Does this mean if I have save 15 properties I need 15 of the assignment lines? I'm already doing essentially what you have and I'm instead wanting to simplify it with (what I'll call) an *"internal clone."*
MikeSchinkel
*@Matt Huggins:* I upvoted for your use of `is_a()` to accept either an object or a string value.
MikeSchinkel
@MikeSchinkel: you can use get_object_vars() to iterate over the object's member variables and get away with 3 lines of code for infinite number of variables.
vls
+2  A: 

In PHP 5, object cloning might be more relevant:

http://php.net/manual/en/language.oop5.cloning.php

You can define a special __clone method.

In PHP 4 and 5, you can copy properties via:

function copy($obj)
{
  foreach (get_object_vars($obj) as $key => $val)
  {
     $this->$key = $val;
  }
}

However, you wrote "I don't want to write code that just copies the properties," and I'm not exactly sure what you mean by that.

konforce
*@konforce*: Thanks for the response. Yes, PHP 5.x would be nice, but nothing makes it into WP core unless v4.x *(that will change eventually.)* That said, I don't think clone is what I'm looking for. Clone makes a copy; I want the constructed object to *"become"* the object passed in like a spy who assumes a new identity! As for *"not just copying properties"* I meant I didn't want *"n"* assignment statements. Your loop is better than just copying properties though I was hoping there'd be a better answer. I'll upvote yours for the use of `get_object_vars()` Thanks.
MikeSchinkel
*@konforce*: Is it possible to assign `$val` by reference, or does the `foreach` simply create a copy? For example, can I write `$this->$key = `?
MikeSchinkel
You'd also have to have `... as $key => `.
konforce
*@konforce:* Picking yours as the *best* answer because you were first to mention `get_object_vars()`. *(It would be good for people reading in the future if you edit to note that `__clone()` in fact does not answer the question whereas `get_object_vars()` was the useful tidbit.)*
MikeSchinkel
+2  A: 

Preferred way of doing this is to use clone keyword and to implement appropriate __clone() method if needed as mentioned by other posters. Another trick way of doing this (con: slow, pros: can be stored, sent over network and works identical in php4/5) is to serialize an object and then unserialize to create new copies of it with identical variable values.

Example:

$productCopy = unserialize(serialize($product));

EDIT: Sorry, misunderstood what you were asking for. You will have to initialize variables of the object being constructed with passed in object's variables inside of the constructor. You can't return a reference to another object from the constructor.

Example:

public function __construct($name, $object = null) { 
    if($object) { 
        foreach(get_object_vars($object) as $k => $v) {
            $this->$k = $v;
        } 
    } else {
        $this->name = $name;
    }
} 
vls
*@vsemenow* Thanks for the response. Unless I completely misunderstand `clone` it's not the answer to what I was asking for. I've used clone before but it creates a copy, it doesn't replace the object currently being constructed with another. Or do I misunderstand `clone?` Also, unserialize creates a copy too so this answer *definitely* does not address the question.
MikeSchinkel
*@vsemenow*: Thanks for confirming that what I want can't be done. The reason is I basically need an handle for my classes that will encapsulate some ugly arrays, and that handle needs to be opaque on the outside. Because of some complexity it can be a string key or an object instance. Once we finally replace the arrays it encapsulates the handle can always simply be a number but until then it cannot. Fortunately it's not a problem to just copy the properties, it's just tedious. But I can use `get_object_vars()` mentioned here to do that.
MikeSchinkel
A: 

Adding another answer due to it being radically different.

$product = new Product('Widget');
$product2 = new Product('Widget #2');
$product =& $product2;
echo $product->name;  // echos "Widget #2"

That should work.

Hunter Bridges
*@Hunter Bridges*: Yes, that works but doesn't address my use-case. Somebody else's code will be in control for the object after the constructor so I don't get the chance to assign it like you show.
MikeSchinkel