The Artima Developer Community
Sponsored Link

PHP Buzz Forum
Scope in PHP

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jackson Miller

Posts: 222
Nickname: jaxn
Registered: Oct, 2004

Jackson Miller is a software developer in Nashvillle, TN currently working at CentreSource.com
Scope in PHP Posted: Dec 18, 2004 9:49 PM
Reply to this message Reply

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.
Latest PHP Buzz Posts
Latest PHP Buzz Posts by Jackson Miller
Latest Posts From Jaxn.org

Advertisement
Why does this work in 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.

Read: Scope in PHP

Topic: Barnstormer and PHP Roundup Previous Topic   Next Topic Topic: Hard drive crash

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use