Question: composition
Moderator: General Moderators
-
networkguy
- Forum Newbie
- Posts: 19
- Joined: Tue Mar 31, 2009 2:26 am
Question: composition
Hi, I am php OOP newbie, please help me out!
What is wrong with instantiating a object through another object by calling the methods of the object returning some string value and storing it in a string of vector/array objects. Is it okay to call this "composition" ?
I looked up many tutorials on composition, one of them looks very weird. Passing a reference from an object when creating a instance of another object which will return something back (string contents etc...).
What is wrong with instantiating a object through another object by calling the methods of the object returning some string value and storing it in a string of vector/array objects. Is it okay to call this "composition" ?
I looked up many tutorials on composition, one of them looks very weird. Passing a reference from an object when creating a instance of another object which will return something back (string contents etc...).
Re: Question: composition
I'm not totally sure what you're asking. Do you have a code example?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
networkguy
- Forum Newbie
- Posts: 19
- Joined: Tue Mar 31, 2009 2:26 am
Re: Question: composition
pickle wrote:I'm not totally sure what you're asking. Do you have a code example?
What I'm asking for is what is meant by proper way to do composition in php.
Why would this NOT work?
//I have tried much more complex code (over 800 lines) and works perfectly using this method
//I have tried with another (see other thread "php code") and didn't work because I used "reference" to pass back string
For example:
Code: Select all
class mainObject {
var $subObject;
var $arrSubObjects = array();
function setObject($author, $id) {
$this->subObject = new subObject($author, $id);
//some other stuff here
}
function addObject() {
array_push($this->arrSubObjects, $this->subObject->getObjectMethod()) //getObjectMethod could return Bob + ID
}
function display() {
foreach($this->$arrSubObjects as $subObj) {
echo "$subObj</br>";
}
}
class subObject
{
//blah blah, you can predict here what method (ie: getObjectMethod() //gets stuff back ie: Bob + ID)
}For some reason when I do it this way(http://www.devshed.com/c/a/PHP/Object-I ... omposition) it doesn't work. They call that way "compsoition", using my way it works but their way it doesn't.
What do you say, if it ain't broke don't fix it? I want to know why mine is not proper compositioon, because what, I am not using reference? This doesn't make any sense when I can instantiate subobjects through my main object and main object becomes composed of subobjects, not the same thing? I am confused here, but nobody seems to be helping here at all, is this OOP forum or still medieval procedural?
Re: Question: composition
It's been ~1 hour since you first posted. This stuff isn't instant you know
What happens with that code? If the goal is to output some strings, then it should work.
What happens with that code? If the goal is to output some strings, then it should work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
networkguy
- Forum Newbie
- Posts: 19
- Joined: Tue Mar 31, 2009 2:26 am
Re: Question: composition
pickle wrote:It's been ~1 hour since you first posted. This stuff isn't instant you know![]()
What happens with that code? If the goal is to output some strings, then it should work.
Yes exactly, just output some string...
So am I doing composition properly since object will be composed of an array of string subobjects, the string subobjects lives and dies of the object. Subobjects don't know what they are, the object gives meaning to them sort of speak...Just like a composer is in charge of the orchestra, they play only when the composer moves its magic wand (whatever the hell it is)
-
networkguy
- Forum Newbie
- Posts: 19
- Joined: Tue Mar 31, 2009 2:26 am
Re: Question: composition
pickle wrote:It's been ~1 hour since you first posted. This stuff isn't instant you know![]()
What happens with that code? If the goal is to output some strings, then it should work.
Sorry if I sound a little crabby, my background is in c programming, i have switched to OOP now and have a headache everyday since that switch...
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Question: composition
Think about the difference between this:
And this:
It is not necessarily that one is better than the other, but the first one sets up a hard dependency with subObject. The second allows polymorphism -- so object of classes that inherit subObject could also be passed. There are cases where one or the other are required. If not then I would recommend the second as it will give you more flexibility down the road.
Also, I would recommend where possible to do constructor initialization. Then you have a usable object upon instantiation.
Code: Select all
class mainObject {
function setObject($author, $id) {
$this->subObject = new subObject($author, $id);
}
}
$main->setObject($author, $id);Code: Select all
class mainObject {
function setObject($obj) {
$this->subObject = $obj;
}
}
$main->setObject(new subObject($author, $id));Also, I would recommend where possible to do constructor initialization. Then you have a usable object upon instantiation.
Code: Select all
class mainObject {
function __construct($obj) {
$this->subObject = $obj;
}
}
$main = new mainObject(new subObject($author, $id));(#10850)
-
networkguy
- Forum Newbie
- Posts: 19
- Joined: Tue Mar 31, 2009 2:26 am
Re: Question: composition
Thank you, good explanation. I don't think I need to utilize polymorphism so I guess I'll just stick with what I have....