Re: Copying an Object

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 9/22/2010 9:11 AM, chris h wrote:
> 
> You could create a method of class B that takes an object of class A as
> a parameter and copies each property line by line (or method of class A
> that takes an object of class B...).  If you don't want to add a method
> you could just do the same thing, but procedurally.  The issue with this
> (aside from being bad oop) is that you can't copy private properties
> unless you have all the required getters and setters.  The issue with
> both of these is that it's ugly, high maintenance code.
> 
> There is the iterator class, extending from which would allow you
> iterate through all of your properties in a foreach, but if you don't
> want to add a new method then you likely don't want to add a parent class.
> 
> I don't care for any of these options, but as far as I know there's no
> internal PHP mechanism to to copy all the properties from one object to
> another object of a different class - please correct me if I'm wrong.
>  Is it possible that there's a more elegant solution to your problem
> that does not include a mass copy of all an object's properties? (e.g.
> using statics like Mr Bungle suggested or perhaps some nifty design
> pattern?)
> 
> 
> Chris H.
> 
> 
> 
> 
> On Wed, Sep 22, 2010 at 7:35 AM, Daniel Kolbo <kolb0057@xxxxxxx
> <mailto:kolb0057@xxxxxxx>> wrote:
> 
>     Hello PHPers,
> 
>     I have:
> 
>     class A {
>            ...code
>     }
> 
>     class B extends A {
>            ...code
>     }
> 
>     $a = new A();
> 
>     $b = new B();
> 
>     I would like to get all of the properties of $a into $b by value.  Class
>     A extends 3 other classes.  I would like a way to not have to manage a
>     'copy' method in B if A or one of the subclasses of A change.
> 
>     I was reading about clone, but this doesn't really seem to help me in
>     this situation.
> 
>     How can I copy $a into $b?
> 
>     Thanks,
>     dK
>     `
> 
> 
>     --
>     PHP General Mailing List (http://www.php.net/)
>     To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
Hello,

Thank you Mr. Bungle, Chris, Nathan, and Carlos Medina.

Nathan, your first response though not exactly what I was looking for
was still instructive to me thanks.  I almost started to implement your
second response but I decided against it as I still wanted class B to
extend class A (and i didn't want the unused members of A to be hanging
around in the objects of B).  Also, I already had __call methods
implemented in the most base class level.  I could have handled this by
calling parent::__call from the child levels if the methods from object
$a were not found.  It would have worked.

Instead I implemented a series of "copy" functions in each of the
extended classes and cascaded through each of the extended classes.
Each copy method calls parent::copy($obj) to copy the elements of the
extended class.

My classes weren't too crazy just 3-5 members each (all of protected
typed) so it'll work for now.

All in all it was a learning curve.  I still think PHP needs to have
this functionality built in.

Say you have two classes: human and male.  Further, say male extends
human.  Let's say you have a human object.  Then later you want to make
that human object a male object.  This seems to be a pretty reasonable
thing to request of our objects.  This type of thing would especially be
easy if objects of parent classes could be cast as an object of its
extended class.

Thanks again for all of your input,
dK
`

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux