[wp-hackers] PHP Coding Practices

Dave Grijalva grijalva at gmail.com
Wed Feb 15 02:14:16 GMT 2006

actually, it is in php4:

:~ dave$ php4
class test {
var $a;
var $b;

$test = new test();
$test->a = 'abc';
$test->b = 'hey, you';

foreach($test as $key => $val){
 echo "$key: $val";
echo "\n";
------  output ------
a: abc
b: hey, you

it may not be documented, but it's there.

yes, this doesn't break iterating over an object, but it does break creating
an object that can be iterated elsewhere.  in PHP5, an iteratable object
should behave properly when used anywhere an array is expected.  typecasting
would break this compatibility all over the wordpress core


On 2/14/06, Robert Deaton <false.hopes at gmail.com> wrote:
> On 2/15/06, Dave Grijalva <grijalva at gmail.com> wrote:
> > That's not really true.  I'm pretty sure you can foreach a PHP4 array.
> Where did this come from? Nobody was debating that. In the event that
> you really meant object, you are wrong, as per http://php.net/foreach,
> object iteration was added in PHP5.
> > <snip>  In either case, it seems foolish to include code that will
> > become a problem in the future, no matter how remote.  Plus, people who
> are
> > running on 5 and want to take advantage of it should be able
> to.  Backwards
> > compatibility is great, but forwards compatibility is a no brainer.
> I am in no way seeing how this would break forwards compatability.
> Anyone who wants to submit a patch that iterates over an object can
> easily do so, leaving out the typecast (maybe even making a comment
> that it is an object so that nobody else comes along and tries to
> typecast it), and continue along with their life.
> We cannot change the behavior of foreach to force the typecast, but we
> can however typecast wherever we know we are expecting an array.
> --
> --Robert Deaton
> http://somethingunpredictable.com
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list