Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    18
    Thanks
    4
    Thanked 4 Times in 4 Posts

    Pass in 1 object but end up with 2

    Greetings,

    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:
    PHP Code:
    <?php

    class A{

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

        public function 
    showArray($arr){

            echo
    '<ol>';

            foreach(
    $arr as $value){
                echo
    '<li>';
                
    print_r($value);
            echo
    '</li><br/>';}

            echo
    '</ol>';
        }

        }



    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>';
    print_r($objA->varA);
    echo
    '</pre><br/><h3>B::getVars Function</h3><br/><pre>';
    print_r($objB->getVars($objA));
    echo
    '</pre><br/><h3>A::showArray Function</h3><pre>';

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

    echo'</pre><br/><br/></body></html>';
    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.

    Thanks,
    Mark

  • #2
    Regular Coder
    Join Date
    Jan 2012
    Posts
    134
    Thanks
    0
    Thanked 32 Times in 32 Posts
    This line:

    PHP Code:
    $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.

  • Users who have thanked KuriosJon for this post:

    cnoevil (02-27-2012)

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    18
    Thanks
    4
    Thanked 4 Times in 4 Posts
    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,
    Mark

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    PHP Code:
    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();
    $col->add('1');
    $col->add('Cat');
    $col->add('Fou-Lu');

    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:
    PHP Code:
    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.

  • Users who have thanked Fou-Lu for this post:

    cnoevil (02-27-2012)

  • #5
    New Coder
    Join Date
    May 2009
    Posts
    18
    Thanks
    4
    Thanked 4 Times in 4 Posts
    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.

    Mark

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #7
    New Coder
    Join Date
    May 2009
    Posts
    18
    Thanks
    4
    Thanked 4 Times in 4 Posts
    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.

    Mark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •