Re: nested, referenced foreach & implicit current array pointer issues

From: Date: Thu, 01 Feb 2007 15:53:52 +0000
Subject: Re: nested, referenced foreach & implicit current array pointer issues
References: 1 2 3 4 5 6 7 8 9 10 11  Groups: php.general 
Request: Send a blank email to php-general+get-248160@lists.php.net to get a copy of this message
On Thu, 2007-02-01 at 16:42 +0100, Roman Neuhauser wrote: > > If PHP was statically typed, global variables would still be a bad > smell. They are bad smell in C++ and Java, for example. It's too easy > to call getfoo() before you have set up $foo. The risk grows > exponentially: as soon as you add another global, $bar, you risk that > you or someone else will use getfoo() inside initbar(), and getbar() > inside initfoo() (or getfoo() inside initfoo()). Of course, it will be > several function calls deep, and quite probably only happen in a code > path that's rarly used (such as error handling). Nopthing wrong with globals as long as they aren't used to punt data around from function to function. I find globals quite useful when used for configuration. I usually use a double level array. The first index is a grouping index such as "someProject" the second index is the name of the property. I could use a database table, but why incur an extra query. I could use a class, but why increase complexity, I could use functions, but complexity again. As for singletons... just use a static class method. <?php class Foo extends Singleton { function Foo() { static $createdAlready = false; if( $createdAlready ) { die( 'Use Foo::getGlobalInstance() instead.' ); } $createdAlready = true; } function getGlobalInstance() { static $singleton = null; if( $singleton === null ) { $singleton = &new Foo(); } return $singleton; } } ?> Now how hard was that!? Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#248160) next »