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



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. |
`------------------------------------------------------------'
.



Relevant Pages

  • Re: [PHP] nested, referenced foreach & implicit current array pointer issues
    ... If PHP was statically typed, global variables would still be a bad ... Nopthing wrong with globals as long as they aren't used to punt data ... I use config arrays, and put all ... class Foo extends Singleton ...
    (php.general)
  • PHP import_request_variables() arbitrary variable overwrite
    ... PHP import_request_variablesarbitrary variable overwrite ... GLOBALS programmers wrote applications using this input method, ... E_NOTICE level error if you specify no prefix, ...
    (Bugtraq)
  • [Full-disclosure] PHP import_request_variables() arbitrary variable overwrite
    ... PHP import_request_variablesarbitrary variable overwrite ... GLOBALS programmers wrote applications using this input method, ... E_NOTICE level error if you specify no prefix, ...
    (Full-Disclosure)
  • Re: static vs global variable
    ... global is kinda trouble as i need to delcare global every php file i ... to have then as globals, in order not to either pass pointers to the ... In short its just as easy to make a buggers muddle in OOP, it jus5 happens with different syntax. ... My pint being that anine using an interepreted language to construct a huge data crunching or screen handling edifice is probably going to be disappointed performance wise anyway, ...
    (comp.lang.php)
  • Re: where do I put global data?
    ... IMK, the singleton instance will be created in heap memory, and static class ... > In what ways is the Singleton pattern better for this (I really want to ... >> contain a Globals singleton, a DatabaseManager singleton, a MapPointWS ... >> private static Globals instance; ...
    (microsoft.public.dotnet.framework.compactframework)