Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- From: robert@xxxxxxxxxxxxx (Robert Cummings)
- Date: Thu, 01 Feb 2007 10:53:52 -0500
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. |
`------------------------------------------------------------'
.
- Follow-Ups:
- Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- From: Németh Zoltán
- Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- References:
- Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- From: Jochem Maas
- Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- From: Roman Neuhauser
- Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- From: Jochem Maas
- Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- From: Roman Neuhauser
- Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- Prev by Date: Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- Next by Date: Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- Previous by thread: Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- Next by thread: Re: [PHP] nested, referenced foreach & implicit current array pointer issues
- Index(es):
Relevant Pages
|