1

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?

flexJoly
  • 136
  • 13
  • 2
    No, and it would be highly illogical if there was.... a static reference cannot ever know what concrete instance or instances there might be of a class – Mark Baker Mar 22 '15 at 13:03
  • The question shouldn't be down-voted, there is no wrong question as soon as an effort has been made to understand / solve the problem. – mins Mar 22 '15 at 13:09
  • But then.... why is there an __callStatic() and not a __getStatic()...? I dont see why we can use all kind of magic methods, but non for this. What am i missing? – flexJoly Mar 22 '15 at 13:15
  • @MarkBaker that is true. But therefore i use singleton for such classes. So it there is only one instance and it is used globally – flexJoly Mar 22 '15 at 13:25
  • It seems i am not the first to ask for this :( http://stackoverflow.com/questions/1279382/magic-get-getter-for-static-properties-in-php The feature-request is from 2009.... – flexJoly Mar 22 '15 at 14:58

1 Answers1

0

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.

bspates
  • 382
  • 1
  • 10
  • Thanks for your answer. In this case that could be helpfull. But i have also parameters which can change. – flexJoly Mar 22 '15 at 13:17
  • If the point is to have a singleton instance you do not need a static accessor method in php. You just need to declare the instance in global space. Then you can access it using the global keyword in other namespaces. http://php.net/manual/en/language.variables.scope.php – bspates Mar 22 '15 at 15:19
  • Global and singleton are not the best practices around. I prefer singleton to global, as i think global is much worse. To get more clean code, i wish i could use something like __getStatic(). – flexJoly Mar 22 '15 at 15:26
  • Well if your heart is set on singleton, is the above snippet missing some code because you static accessor method is missing some logic. Check out the singleton example here http://www.phptherightway.com/pages/Design-Patterns.html – bspates Mar 22 '15 at 15:35
  • Thanks for your tip. I did not write that part in my question, because i thought it would make the example too long. And the singletons are working nicely. Sorry for not mentioning – flexJoly Mar 22 '15 at 15:56