From: Daniel Kolbo on
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
`

From: mr bungle on
Maybe statics members can help you with this ...

On Wed, Sep 22, 2010 at 07:35, Daniel Kolbo <kolb0057(a)umn.edu> 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
>
>


--
Ellis 1100101000111101
From: chris h on
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(a)umn.edu> 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
>
>
From: Nathan Nobbe on
On Wed, Sep 22, 2010 at 5:35 AM, Daniel Kolbo <kolb0057(a)umn.edu> 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.


first off you have to be careful here since there is no real explicit copy
by value in php since internally the engine uses copy on write. there is a
caveat in __clone however, where you can force new instances of objects to
be created in cloned instance variables rather than a reference to the
original object in member variables of the cloned instance.


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

well since B extends A directly here, obviously theres no need to worry
about other subclasses of A changing when considering the copy constructor
(__clone in php) of B. anyway if A changes you need only potentially revise
the __clone() implementation of A.


> I was reading about clone, but this doesn't really seem to help me in
> this situation.
>

egh? define __clone() in A to be responsible for any member variables
defined in A then define __clone() in B to be responsible for any additional
member variables defined therein, making sure to invoke parent::__clone()
from B. naturally if you add or remove member variables from A you revise
the __clone() implementation of A, the same is true for B.

this isnt a maintenance nightmare and is also a sound oop practice. the
same idea would be applied to a copy constructor in java or c++.

note also that php internally manages a shallow copy out of the box, meaning
you wont have to implement any code to 'copy' non-object member variables in
cloned objects. you will however have to determine if a reference to the
same objects in instance variables of cloned objects is appropriate or if
each instance of the class you are cloning needs to have their own instance
of member variables referencing objects.

here is a somewhat contrived example which illustrates everything. all you
have to do is run the script and inspect the id of each object stored in
member variables. what youll find is that in each instance of B shallowObj
is referring to the same instance of stdClass, yet anotherObj from B and
someObj from A are each explicitly 'copied' for each instance of B. also
youll notice that A manages cloning of someObj and B manages cloning of
anotherObj, keeping in line with encapsulation. also notice the scalar
variables are 'copied' by the engine w/ no work in userspace.

<?php
class A
{
public $blah = 0;
protected $meh = 1;
private $care = 2;
private $someObj = null;

public function __construct()
{
$this->someObj = new stdClass();
}

public function __clone()
{
// note only if we want to force a new instance of
// stdClass into $this->someObj in cloned instance of A
$this->someObj = clone $this->someObj;
}
}

class B extends A
{
protected $what = 5;
protected $anotherObj = null;
protected $shallowObj = null;

public function __construct()
{
parent::__construct();
$this->anotherObj = new stdClass();
$this->shallowObj = new stdClass();
}

public function __clone()
{
// make sure to invoke parent __clone()
parent::__clone();

// note as in A::__clone() above, forcing new instance
// of $this->anotherObj in cloned instance of B
$this->anotherObj = clone $this->anotherObj;

// note also that we do not clone $this->shallowObj here,
// meaning each instance of B will reference the same instance of
$this->shallowObj
}
}

$b1 = new B();
$b2 = clone $b1;

var_dump(array(
$b1, $b2
));
?>

/// output
array(2) {
[0]=>
object(B)#1 (7) {
["what":protected]=>
int(5)
["anotherObj":protected]=>
object(stdClass)#3 (0) {
}
["shallowObj":protected]=>
object(stdClass)#4 (0) {
}
["blah"]=>
int(0)
["meh":protected]=>
int(1)
["care":"A":private]=>
int(2)
["someObj":"A":private]=>
object(stdClass)#2 (0) {
}
}
[1]=>
object(B)#5 (7) {
["what":protected]=>
int(5)
["anotherObj":protected]=>
object(stdClass)#7 (0) {
}
["shallowObj":protected]=>
object(stdClass)#4 (0) {
}
["blah"]=>
int(0)
["meh":protected]=>
int(1)
["care":"A":private]=>
int(2)
["someObj":"A":private]=>
object(stdClass)#6 (0) {
}
}
}

hope it helps!

-nathan
From: Nathan Nobbe on
On Wed, Sep 22, 2010 at 5:35 AM, Daniel Kolbo <kolb0057(a)umn.edu> wrote:

> Hello PHPers,
>
> I have:
>
> class A {
> ...code
> }
>
> class B extends A {
> ...code
> }
>
> $a = new A();
>
> $b = new B();
>

after looking back over your situation, which looks strange, why not use
composition at this level,

// note untested, may have some syntax errors
class B
{
private $a = null;

public function __construct(A $a)
{
$this->a = $a;
}

public function swapA(A $a)
{
$this->a = $a;
}

// proxy any calls to A which you would like to appear transparent
public function __call($method, array $args)
{
return call_user_func_array(array($this->a, $method), $args);
}

// note requires 5.3
public function __callStatic($method, array $args)
{
return call_user_func_array(array($this->a, $method), $args);
}
}

$a = new A();
$b = new B($a);
// some code ..
$anotherA = new A();
// some more code
$b->swapA($anotherA);

-nathan