PHP magic methods

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by callumacrae, Nov 9, 2010.

  1. callumacrae

    callumacrae not alex mac Community Support

    Messages:
    5,257
    Likes Received:
    97
    Trophy 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
     
  2. misson

    misson Community Paragon Community Support

    Messages:
    2,572
    Likes Received:
    72
    Trophy Points:
    48
    Not at the language level. As the docs say,
    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: Nov 9, 2010
  3. callumacrae

    callumacrae not alex mac Community Support

    Messages:
    5,257
    Likes Received:
    97
    Trophy 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
     
  4. Submariner

    Submariner New Member

    Messages:
    44
    Likes Received:
    1
    Trophy 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.
     
  5. misson

    misson Community Paragon Community Support

    Messages:
    2,572
    Likes Received:
    72
    Trophy 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?
     
  6. callumacrae

    callumacrae not alex mac Community Support

    Messages:
    5,257
    Likes Received:
    97
    Trophy Points:
    48
    I'm aiming for my script to support PHP 5.2 anyway, so it looks like a no :(

    ~Callum
     

Share This Page