PHP magic methods

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
So I'm like killing myself here.

I want to use __get, but while static. There is a _callStatic, but no __getStatic. Is there any way round this? Currently I'm using:

PHP:
        public static function get($name)
        {
                if(isset(self::$instances[$name]))
                {
                        return self::$instances[$name];
                }
                return false;
        }

It doesn't look great :(

I don't want to be using a function, and I don't want to construct the class. Is there any way to do this?

~Callum
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I want to use __get, but while static. There is a _callStatic, but no __getStatic. Is there any way round this?
Not at the language level. As the docs say,
Property overloading only works in object context. These magic methods will not be triggered in static context.
The solutions are to use a function you explicitly call (as you're doing now) or to extend PHP to add support __getStatic and __setStatic magic methods and submit a patch.
 
Last edited:

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
It's strange that they includes __callStatic but not an equivalent for get :/

Anyway I don't know C, so I guess I'm stuck with a function to do it :(

~Callum
 

Submariner

New Member
Messages
44
Reaction score
1
Points
0
If your value is not going to change, why not use a constant? Updating a static member from any of the instances results in having to implement locking to ensure only one instance is writing to it at a time.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
@Alex: it looks like a bug report (and patch) for this was filed awhile ago (2 years, at this point), but has yet to be included into the codebase.

@Submariner: since standard PHP doesn't support concurrency within the language, simultaneous access of PHP variables shouldn't be a problem (simultaneous access of non-PHP resources is another matter), n'est-ce pas?
 

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
I'm aiming for my script to support PHP 5.2 anyway, so it looks like a no :(

~Callum
 
Top