NYCPHP Meetup

NYPHP.org

[nycphp-talk] OO - when classes are related, who gets which method?

Hans Zaunere zaunere at yahoo.com
Wed Jun 11 11:19:45 EDT 2003


--- Chris Snyder <chris at psydeshow.org> wrote:
> I've got a question for anyone who's an old hand at object-oriented 
> programming, or for any new hands with opinions:

That's me! (the latter)

> Let's say I have a class named Object, and I want to create another 
> class that is related to it named Related. I figure I can construct 
> Relateds in either of two ways:
> 
> $related = $object->createRelated();
> or
> $related = new Related; $related->create(&$object);
> 
> Is there a general rule of thumb that will tell me which is the better 
> way to implement this?
> 
> The first looks simpler, but something tells me the second is better 
> form, because it keeps the creation method inside the class.

But don't they do different things?  The first case returns a Related object,
while the second associates $object with an object of class Related.  I
suppose, depending on how the classes operate internally, the result could be
the same, but from just reading the snippet of code, my procedural brain
would assume something slightly different is happening between the two  :)

Oddly enough, I've been working a lot with objects, and have contemplated
similar things.  You're probably aware of this:

http://us4.php.net/manual/en/ref.objaggregation.php

but it was a new section in the manual for me (just goes to show how long
it's been since I've dealt with objects  :)

H






More information about the talk mailing list