SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Rotterdam
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling two methods after each other in PHP4?

    Something's confusing me... I have this piece of code:
    PHP Code:
    $stmt $dbh->prepare($query);
    $result $stmt->execute('Joe'); 
    ... which works fine.

    Then I figured I could shorten it a bit by doing this:
    PHP Code:
    $result $dbh->prepare($query)->execute('Joe'); 
    ... which gives me a parse error:
    Code:
    parse error, unexpected T_OBJECT_OPERATOR in [...] on line 9
    I have seen this being done in the book "Advanced PHP Programming", but all the code in there is written for PHP5. Is what I'm trying to above not possible in PHP4?

    I'm sorry to have to ask about basic PHP syntax, but I could not find this in the manual, partly because I find it hard to put this into words.

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think, your
    prepare($query)
    isnīt an Object.
    It is simply a method, so you canīt apply an object operator.

    I donīt know, if it works in PHP5 .. perhaps it takes the return value of prepare as if it is an object and applys the execute method.

    Hth, and please correct me, if Iīm wrong.

    Bye, Transmitter

  3. #3
    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)
    The syntax you showed will work fine under php5's object model, but with php4 you have to have the temporary variable with the object.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Rotterdam
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Transmitter
    I think, your prepare($query) isnīt an Object.
    It is simply a method, so you canīt apply an object operator.
    I checked it, and the prepare method does in fact return an object. The class of that object in turn implements the execute() method, which is why the first block of code works.

    Quote Originally Posted by sweatje
    The syntax you showed will work fine under php5's object model, but with php4 you have to have the temporary variable with the object.
    Yes, it just seems like it needs the $result variable to 'remember' the object returned by prepare().

    I read something about the difference in how 4 and 5 handle variables (copy vs. reference), which might have something to do with this. Then again, it may not.

    Thanks to you both, Transmitter and sweatje, for helping me clear this up. I was afraid I was making some kind of mistake in my logic, but it seems this is simply a difference between PHP 4 and 5.

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2002
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The zend php5 changes document documents this


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
  •