This post originated from an RSS feed registered with PHP Buzz
by Jackson Miller.
Original Post: Scope in PHP
Feed Title: Jaxn.org
Feed URL: http://jaxn.org
Feed Description: PHP thoughts on architecture and application design. Includes development information about Core Enterprise PHP.
if (isset($bar)) {
$foo = new Foo($bar);
}
else {
$foo = new Foo($baz);
}
$foo->doSomething();
Working with Java daily, this PHPism is a habbit that bites me in the ass too frequently. For those who don't know this is how you do it in Java:
Foo foo = null;
if (null != bar) {
foo = new Foo(bar);
}
else {
foo = new Foo(baz);
}
foo.doSomething();
The reason for this is that anything that is within curly braces is in it's own scope by default.
Java is like other languages and PHP seems to be the anomoly.
This seems to be a lasting remnant of PHP's initial decision to make everything global. Now register_globals can be turned on (and is on by default) which fixes most of the global problems, but not this one.
I don't think that I really prefer one over the other, but I wonder why PHP seems to be the only one that does it this way.