objects that interact with each other

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

objects that interact with each other

Post by s.dot »

Consider the following example:

Code: Select all

<?php

class people
{
	private $people
	
	function doSomething()
	{
		foreach ($this->$people AS $person)
		{
			//
		}
	}
	
	function addPerson($person)
	{
		$this->people[] = $person;
	}
}

class person
{
	private $name;
	private $age;
	private $etc;
	
	function setName($name){}
	function setAge($age){}
	function setEtc($etc){}
}

$people = new people();

$bob = new person();
$bob->setName('bob');
$bob->setAge(33);
$bob->setEtc('etc');

$people->addPerson($bob);
Should person class extends people class and perhaps have a method called add()? which adds it to the people property? Or should it be two separate classes like I have it above?

EDIT| This is pseudo-code.. it's just the concept I'm after.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: objects that interact with each other

Post by Jenk »

"Does X extend or override the behaviour of Y?" is the only question need asking here. :) If yes to either, it's a subclass.

Could also try Composition instead of inheritance. A bit more tricky, but usually more elegant.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: objects that interact with each other

Post by s.dot »

That is a good question, lol.
It does not extend or override - but it provides information to

Maybe I don't need an additional class?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: objects that interact with each other

Post by Eran »

Notice what Jenk mentioned - composition. If your People class is a collection of Person classes, then its probably best to use composition instead of inheritance. Of course, when dealing with pseudo code it's rather meaningless - those decisions need to be based on the relationships in the actual application.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: objects that interact with each other

Post by s.dot »

OK.

The "people" object uses the collection of "person" objects to manipulate data supplied to the "people" class.

I'll just do what I think is best and post it in the critique forum.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: objects that interact with each other

Post by josh »

what you are doing is fine, unless you have too much of 'feature envy' code smell http://c2.com/cgi/wiki?FeatureEnvySmell
Post Reply