NYCPHP Meetup

NYPHP.org

[nycphp-talk] overriding strval(x)?

Adam Maccabee Trachtenberg adam at trachtenberg.com
Fri Jan 23 02:26:12 EST 2004


On Fri, 23 Jan 2004, jon baer wrote:

> Also, have you tried doing
> > foreach on the object?
> >
>
> can u explain? is this built-in introspection or did u mean on my own with
> my own method?

This works in PHP 4 and PHP 5:

class foo {
   var $a = 1;
   var $b = 'php';
}

$foo = new foo;
foreach ($foo as $k => $v) {
    print "$k: $v\n";
}

a: 1
b: php

I don't remember how PHP 5 modifies this to account for private and
protected variables, but it should be easy to check. :) Also, if
you're storing variables and arrays in the properties, you'll need to
do a print_r() on them or otherwise check their type and handle them
accordingly.

There's also the PHP 5 introspection functions, but I won't be getting
around to reading up on that until next month, so you're on your own
here. Meanwhile you could check out the January 2004 issue of Linux
Magazine (I picked up a free copy at LinuxWorld Expo) or check out:

http://sitten-polizei.de/php/reflection_api/docs/language.reflection.html

-adam

-- 
adam at trachtenberg.com
author of o'reilly's php cookbook
avoid the holiday rush, buy your copy today!



More information about the talk mailing list