From: Carlos Medina on
Am 22.09.2010 13:35, schrieb Daniel Kolbo:
> 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
> `
>
Hi dk,
by using inheritance only you will bind your code to an specific
implementation of code. Wherever you want to do with your classes, i
think is the better way to use some design patterns to build classes by
the functionality or by structure.
The use of composition allow to manage your classes flexible very well.
But look for Factory or Decorator (maybe Command in this case) to solve
the class inheritance.

Regards

Carlos Medina
From: Daniel Kolbo on
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(a)umn.edu
> <mailto: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
>
>
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
`
From: Peter Lind on
On 23 September 2010 02:14, Daniel Kolbo <kolb0057(a)umn.edu> wrote:

*snip*

> On 9/22/2010 9:11 AM, chris h wrote:
> 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.

Perhaps if you're a C# programmer, but the PHP way of thinking is
radically different.
C#: This object is whatever it was currently cast to (if possible)
PHP: This object is this object, whatever it was created as

If you have a need to make an object switch class in PHP, then there's
a 99% chance you're working against, not with the language.

>  This type of thing would especially be
> easy if objects of parent classes could be cast as an object of its
> extended class.

I'll hazard a guess and say you didn't start programming in PHP but in
something else.

Regards
Peter

--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
BeWelcome/Couchsurfing: Fake51
Twitter: http://twitter.com/kafe15
</hype>
From: Carlos Medina on
Am 23.09.2010 08:24, schrieb Peter Lind:
> On 23 September 2010 02:14, Daniel Kolbo<kolb0057(a)umn.edu> wrote:
>
> *snip*
>
>> On 9/22/2010 9:11 AM, chris h wrote:
>> 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.
>
> Perhaps if you're a C# programmer, but the PHP way of thinking is
> radically different.
> C#: This object is whatever it was currently cast to (if possible)
> PHP: This object is this object, whatever it was created as
>
> If you have a need to make an object switch class in PHP, then there's
> a 99% chance you're working against, not with the language.
>
>> This type of thing would especially be
>> easy if objects of parent classes could be cast as an object of its
>> extended class.
>
> I'll hazard a guess and say you didn't start programming in PHP but in
> something else.
>
> Regards
> Peter
>
Hi,
i think the problem here is the casting posibility in PHP is not
implemented yet. Like Java you can not cast objects to others. I found,
dass Classes can be casted by using this code:

class ParentClass
{
public static function cast( ParentClass $object )
{
return $object;

}

public function tellMee( $value )
{
echo $value;
}
}

class childClass extends ParentClass
{

}

$parent = ParentClass::cast( new childClass());
$parent->tellMee( 'Hallo ');
var_dump($parent);

Well yes you can get the ChildClass by using the parent class and use
the methods allowed to use in the parent class (as method of parentClass
in childClass). But you can not use inheritance in this construct in
PHP. I think the best way to work with PHP classes is by using the
Design Patterns for PHP...

Regards

Carlos
From: David Hutto on
On Fri, Sep 24, 2010 at 4:09 AM, Gary <php-general(a)garydjones.name> wrote:
> Daniel Kolbo wrote:
>
>> 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.
>
> I don't think any human can change gender without major surgery, but I
> don't know if you just chose your example badly or whether you really
> think objects should be able to mutate into other types of object
> without some kind of special treatment.

But it would work in something like makehuman, where you start with a neuter
form and scale one way or the other for physical features. If I
remember correctly,
we're' all xx until you become xy(genetically speaking).

>
>> This type of thing would especially be
>> easy if objects of parent classes could be cast as an object of its
>> extended class.
>
> Where would the extra data come from to fill in any fields the base
> class does not have? Just think of a simple example with a Shape class,
> extended by a ColouredShape class which contains some data about the
> object's colour - if you have a Shape object it can't become a
> ColouredShape without some surgery because bits of the ColouredShape's
> anatomy are not present.
>
> --
> Gary        Please do NOT send me 'courtesy' replies off-list.
> PHP 5.2.12 (cli) (built: Jan 14 2010 14:54:11)
> 1.7.7(0.230/5/3) 2010-08-31 09:58 Cygwin
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: ZipArchive, but without files
Next: module add on Suse 10.3