Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAnas Idris
2,920 Pointshow does using the return keyword directly on the $db object work?
the $db->query() return PDO::Statement object on success so how is it possible that we could use the returned object in foreach loop as an array? and when I var dumbed get_project_list(), the result was: object(PDOStatement)#2 (1) { ["queryString"]=> string(48) "SELECT project_id, title, category FROM projects" } and not an array containing the items in the projects table!
1 Answer
Xavier Miranda
12,754 PointsI was baffled when I also saw this. Apparently foreach() iterates over arrays AND objects. Following quotes from php.net
The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects[...]
It is possible to customize object iteration.
At this stage I can't fully explain why that is, but at least the documentations assures me this is not black magic
Cheers.
Andrew Dickens
18,352 PointsAndrew Dickens
18,352 PointsThat sort of blows my mind that you can foreach an object.. thanks for finding that out, has saved me a lot of trouble