View Full Version : Pass in 1 object but end up with 2

02-27-2012, 04:03 AM

I'm in the process of trying to learn how to handle objects in php. It seems to be a little trickier than I expected. I've been working with 2 simple classes to try and get the hang of passing one object to a method in another without choking on it. I've finally managed to get that to happen, but my method in class A creates 2 objects even though when I print out a similar call from class B it shows that only 1 object is being passed in. I've put a comment next to the line that produces the issue. I think... Here's the code:


class A{

public $varA = array(tom,dick,harry);

public function showArray($arr){


foreach($arr as $value){



class B{

public function getVars($obj){

$this->varB[] = $obj;

return $this->varB; }


$objA = new A();

$objB = new B();

echo'<html><head><title>Object Passing Class Test</title></head>';
echo'<body><h1>Passing Objects</h1><br/><br/>';

echo'</pre><br/><h3>B::getVars Function</h3><br/><pre>';
echo'</pre><br/><h3>A::showArray Function</h3><pre>';

$objA->showArray($objB->getVars($objA));//<-This call right here produces 2 objects


What am I missing? Also, does anyone know of some really good in depth tutorials with examples on object wrangling in php that can be found online? I sure would like to get the hang of this sometime in this lifetime.:eek:


02-27-2012, 12:46 PM
This line:

$this->varB[] = $obj;

The [] is an array assignment. Every time that line of code runs, you're adding a new element to the array varB. Drop the square brackets and it will run fine.

02-27-2012, 06:46 PM
Thanks KuriosJon,

I learned something from this. I spent 2 days reading just to figuring out how to get the method to spit out the object without causing an error, but when I made the final change that got me some success I changed 3 different things. Well, then it 'kinda' worked but I wasn't sure which change did what. Now I've decided that it's best to change one thing, then test, then change another...

Also, I was reading somewhere - and now I've lost it - that in order to read out the elements residing in array variables within objects that you should implement some variety of iterator interface. Is this so? If so, is there a particular one that I should implement? Or is it better to use one of the iterator classes in the SPL? Or what?

How about this, what do you think is the best way to handle pulling values from an array inside of an object?

Thanks Again,

02-27-2012, 07:22 PM
That's only if your class is to be a Traversable type. You'd need to implement Iterator or IteratorAggregate. IteratorAggregate works if the underlying is already a traversable type, which in the case of an Array, sort of is, but you need to invoke the iterator on it.

class MyCollection implements IteratorAggregate
private $aCollection = array();

public function add($in)
$this->aCollection[] = $in;

public function getIterator()
return new ArrayIterator($this->aCollection);

$col = new MyCollection();

foreach ($col AS $item)
print $item . PHP_EOL;

One thing to note is that PHP's automagic creation of member properties is undesirable. You should be forced to provide all of your properties during link and not during runtime. Using:

public function __set($a, $b)
throw new Excpetion('No such property');

will prevent you from creating magic members.

Also, as for your original question, you don't have two objects. You only have one object with two object pointers to it. If you change it in one, you end up with the change reflected in both. They are not passed by reference so you cannot modify the underlying pointer with a new instantiation, but the actual objects themselves are still the same. You could add 100 of them, but its still just a single instance.

02-27-2012, 09:22 PM
Thanks Fou-Lu. IteratorAggregate...that's exactly the one I was looking at, I just couldn't remember what it was. It's been a bumpy ride trying to grasp how oop works in php. It's different in some subtle ways from how it's implemented in more strongly typed languages like java and c#. And although I'm no professional in any of them, I started out learning on java and c# so I've spent more time on strongly typed languages.


02-27-2012, 09:37 PM
PHP's approach is very similar to Java's minus the datatype support. PHP does support typehinting which certainly helps, but you cannot use wildcarding - only inheritable types.
PHP's OO is certainly juvenile compared to a language like Java. Although given that its only a few years old, I can't wait to see how it unfolds to retain both the OO and procedural approaches.

02-27-2012, 10:25 PM
I think my biggest issue with php, which is even more fundamental in nature than the way it implements OO, is dynamic typing. Javascript has been giving me fits for the same reason.

I keep getting blindsided by variables that seemingly come out of nowhere. I try to read other people's code to learn the ins and outs and sometimes I just don't get that a variable is being created on the fly in the middle of an expression. I've spent hours pulling through a code base looking for where a variable was defined only to discover that...bingo, that was it, right there in the middle of that expression. You can even shoot yourself in the foot by misspelling a variable and have a new one that doesn't cause any error but none the less your code doesn't work and you don't know why.

It's a different mind set... but I'm still having fun with it and I hope to get a handle on it enough to be able to do something with it. Thanks for your help.