SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing array of objects

    Can anyone help me to get this work or verify if its a bug? I have been playing around with this and is getting frustrated. Thanks

    PHP Code:
    class A
    {
        function 
    A($arrayobj) {
            while(list(
    $key$value) = each($arrayobj))    {
                echo 
    $value->spit();
            }
        }
    }

    class 
    {
        function 
    spit() {
            return 
    'This is class B' "\n";
        }
    }

    class 
    {
        function 
    spit() {
            return 
    'This is class C' "\n";
        }
    }

    new 
    A( array( new B(), new C())); 
    I got this error:

    This is class B
    Fatal error: Call to a member function spit() on a non-object in

    B class seems to work but not C

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    instead of your while loop, why not just:
    PHP Code:
    foreach($arrayobj as $obj) {
        
    $obj->spit();

    ?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I already tried every loop, but still falls the same result

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting, looks like you may have stumbled on a PHP bug! Take a look at the var_dump of $arrayobj the way you are calling it. Then changed you construction of A to be:
    PHP Code:
    $b = new B$c = new C;
    new 
    A( array($b$c)); 

  5. #5
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    Interesting, looks like you may have stumbled on a PHP bug! Take a look at the var_dump of $arrayobj the way you are calling it. Then changed you construction of A to be:
    PHP Code:
    $b = new B$c = new C;
    new 
    A( array($b$c)); 
    Ok, so this long winded alternative seems to work.

    One more interesting nightmare here:

    PHP Code:
    class 

        function 
    A($value) { 
                 echo 
    $value->spit(); 

        } 


    class 

        function 
    spit() { 
            return 
    'This is class B' "\n"
        } 


    new 
    A( new B()); 
    Resut: Apache Crash!

    Thanks sweatje.

  6. #6
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So anyone want to report this bug or if not, can anyone lead me to the page for submitting? I can't find the place here: http://bugs.php.net/how-to-report.php

    EDIT:: I found it

  7. #7
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For anyone who wants to keep track of this, the URL for this report: http://bugs.php.net/30080

  8. #8
    SitePoint Addict pointbeing's Avatar
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by say
    For anyone who wants to keep track of this, the URL for this report: http://bugs.php.net/30080
    Fwiw, I can reproduce both of these with 5.00 + Fedora Linux.

    But if I change the last line of either, to assign the new A to a variable, like so:

    PHP Code:
    <?php

    $a 
    = new A( array( new B(), new C()));

    //or ...

    $a = new A( new B());

    ?>
    that all works nicely. I don't know what this tells us but I thought I should mention it

  9. #9
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In a project at work I am having millions of problems with PHP 4.3.7 and it's internal variable referencing system.

    One thing I have is a tree of classes arranged much like DOM (the member childNodes is a array of child objects that extend the node interface which defines the blue print for the composite pattern I use to process nodes).

    With this tree I find that each object I have also has a unique ID which is referenced in a Singleton. I find some object types don't get referenced in the array member in my Singleton, but instead it makes a copy of the internal stdClass instead of referencing my variables.

    Pavel, the LIMB developer also had similar problems in PHP5. You supposedly can get away with not using & to reference objects in PHP5, however him and I discovered this wasn't always the case as his code seemed to work when I suggested explictly specifying a reference in his code.

    -- Jason

  10. #10
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to point out this works for me, but I came across similar bugs to this in some other code of mine

  11. #11
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    works for me in 4.3.8 but not 5.0.1

    this also makes it go away in 5.0.1

    PHP Code:
    <?
    $b
    =new B(); $c= new C() ;
    new 
    A( array( $b $c ) ) ; 
    ?>
    ?

  12. #12
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by firepages
    works for me in 4.3.8 but not 5.0.1

    this also makes it go away in 5.0.1

    PHP Code:
    <?
    $b
    =new B(); $c= new C() ;
    new 
    A( array( $b $c ) ) ; 
    ?>
    ?
    Yup, as what sweatje had posted above. Lame isn't it?

  13. #13
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup, as what sweatje had posted above. Lame isn't it?
    DOH , didnt spot that

  14. #14
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suppose it is reasonably easy to see how this slipped though as a bug, how much utility is there in an "initialize only, never use" object anyway

    I have not tested it, but I suspect the code will work if you call it as:
    PHP Code:
    some_function(new A(array(new B, new C))); 
    becuase the reference has somewhere to land, namely the input parameter for the function.

  15. #15
    SitePoint Addict pointbeing's Avatar
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    I have not tested it, but I suspect the code will work if you call it as:
    PHP Code:
    some_function(new A(array(new B, new C))); 
    becuase the reference has somewhere to land, namely the input parameter for the function.
    I've tested it and you're absolutely right - it works just fine


Bookmarks

Posting Permissions

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