Is there a magic method to access a non-static property statically?

406 Views Asked by At

I hope this is not a stupid question, but i really can't find an answer.

I have some global classes with a singleton function. Mostly it is about small config parameters.

class myConfig{
   protected $strQuote = '"';
   protected $path_delimiter = '\\';

   public function __get($name){
        return $this->$name; // after checking if it exist etc.
   }

   public static function getMe(){
        // do the singleton magic
        return $oInstance;
   }

}

this works fine:

$quote = myConfig::getMe()->strQuote;

this also:

$oConf = myConfig::getMe();
$quote = $oConf->strQuote;
$delim = $oConf->path_delimiter;

But mostly just 1 small parameter is needed which i would like to address as:

$quote = myConfig::$strQuote;

For everything are magic methods, but I cannot find any to solve this. I have tried static __get() and __callstatic(). But cannot get it to work.

Declaring the properties as static is not an option. For the class will be used mostly as an instance.


UPDATE workaround

I just got the idea of a dirty workaround. I am not sure if it is too dirty.

$quote = myConfig::strQuote();

or

$quote = myConfig::get_strQuote();

And then handle it with __callStatic()

Is this too dirty?

1

There are 1 best solutions below

5
On

Instead of using a static variable just make $strQuote a constant var. then you can access it in the same manor as a static variable. const STR_QUOTE = "'"; then you can access it in the class as self::STR_QUOTE and externally with the class name.