Globals!? YUCK :) A better solution IMHO that maintains encapsulation,
is to use a static var in a function:

function &getClassVar( $name )
{
    return getAndSetClassVar( $name, false );
}

function &setClassVar( $name, $value )
{
    return getAndSetClassVar( $name, true, $value );
}

function &getAndSetClassVar( $name, $set, &$value )
{
    static $classVars = array();

    if( !$set )
    {
        return $classVars[$name];
    }

    $classVars[$name] = &$value;

    return $value;
}

Admittedly this is slower than using a global, but it doesn't pollute
the global scope and ensures you'll never have naming conflicts.

HTH,
Rob.

On Thu, 2003-08-28 at 13:48, Tom Rogers wrote:
> 
> static calls to a class will never have $this set so you have to set
> your reference globally something like this:
> 
> <?php
> $_miInstancia = array();
> class db{
>         var $c;
>         function db ($count=false){
>                 global $_miInstancia;
>                 $_miInstancia['db'] =& $this;
>                 if($count)$this->c = $count;
>                 // funcion que se conecta con la BBDD
>                 $result = @mysql_pconnect("inforalv", "discoteca", "password");
>                 if (!$result)
>                         return false;
>                 if ([EMAIL PROTECTED]("discoteca"))
>                         return false;
>         }
>         function &getInstancia($count=false){
>                 global $_miInstancia;
>                 if (!isset($_miInstancia['db'])){
>                         new db($count);
>                 }
>                 return $_miInstancia;
>         }
> }
> $conexiondb =& db::getInstancia(1);
> print_r($conexiondb);
> $conexiondb2 =& db::getInstancia();
> print_r($conexiondb2);
> 
> ?>
> 
> (The $count was just to prove we only have the one instance)
> -- 
> regards,
> Tom
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
-- 
.---------------------------------------------.
| Worlds of Carnage - http://www.wocmud.org   |
:---------------------------------------------:
| Come visit a world of myth and legend where |
| fantastical creatures come to life and the  |
| stuff of nightmares grasp for your soul.    |
`---------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to