#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    104
    Rep Power
    4

    PHP OOP iteration


    I am learning oop and can make a foreach() loop to iterate over each property outside of the class instead of typing
    echo $myobject->get_firstName;
    echo $myobject->get_lastName;

    What if I want to iterate over all the objects I make from the class like if they were rows of data in a datafeed of Hotel guests?

    Person1 - Peter, Pan
    Person2 - Mike, Walsh
    Person3 - Allan Jones
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    Originally Posted by ottz0
    I am learning oop and can make a foreach() loop to iterate over each property outside of the class instead of typing
    echo $myobject->get_firstName;
    echo $myobject->get_lastName;
    As an experienced programmer: outside of a debugging situation, foreaching over an object to get its properties is useless. And frankly, it doesn't make sense to do so either. If I had control over PHP I'd remove that capability entirely.

    Originally Posted by ottz0
    What if I want to iterate over all the objects I make from the class like if they were rows of data in a datafeed of Hotel guests?
    You make an array of objects. Or create a special object representing a collection of people, make it implement Iterator (or a similar interface), and loop through that.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    104
    Rep Power
    4
    Thanks for that. To me it's like the chicken or the egg. What starts first? As person1, person2 is essentially another object.

    "Iterate and loop through that", exactly. How do you use this then to print a list of hotels each with its own feature like from an XML file? How can you use classes to look just inside the hotel list and the through the properties of a specific hotel? How would you build OOP with a list view and detail view for example? Do you know of any tutorials as I can't find any anywhere
  6. #4
  7. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,327
    Rep Power
    2063
    For iterating over an array of objects, here's a very much simplified example:

    PHP Code:
    $person_1 = new Person ();
    $person_2 = new Person ();
    $person_3 = new Person ();

    $people = array (
        
    $person_1
        
    $person_2
        
    $person_3
    );

    foreach (
    $people as $person) {
        echo 
    $person->getName ();

    And I agree 100% with requinix. You don't iterate through an object to get it's properties. You should now the properties before you start off. That's what objects are for.

    To do anything more then this you'd need to actually go and try a few things and see what works. OO is not that hard when you understand it, but it can take a little bit of time to fully understand the concept of how it all works in together.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    28
    Rep Power
    0
    To add something here I would say call the object as prescribed in the PHP manule

    PHP: sqlsrv_fetch_object - Manual

    I hope this would help as bit!

    Comments on this post

    • Catacaustic disagrees : What does an SQL Server function have to do with learning basic OOP??
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    28
    Rep Power
    0
    @catacaustic

    what is wrong in my ans can you explain i just given link about the object relation ship and not the basic oop learning
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,082
    Rep Power
    379
    Your link is talking about Database connection... this has nothing to do with OOP.. its like me asking about walking and you talking about running or hopping..

    Comments on this post

    • Catacaustic agrees : Very good explanation. But a bit more ike the OP asking about walking and a reply about learning to fly...

IMN logo majestic logo threadwatch logo seochat tools logo