+ Reply to Thread
Results 1 to 6 of 6

Thread: PHP magic methods

  1. #1
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    PHP magic methods

    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 Code:
            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
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP magic methods

    Quote Originally Posted by Alex Mac View Post
    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 by misson; 11-09-2010 at 08:25 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: PHP magic methods

    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
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  4. #4
    Submariner is offline x10Hosting Member Submariner is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    TN, USA
    Posts
    44

    Re: PHP magic methods

    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. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP magic methods

    @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. #6
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: PHP magic methods

    I'm aiming for my script to support PHP 5.2 anyway, so it looks like a no

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

+ Reply to Thread

Similar Threads

  1. Patterns and Methods used in these photos
    By espfutbol98 in forum Graphics & Webdesign
    Replies: 0
    Last Post: 10-14-2010, 05:02 PM
  2. Payment methods
    By macnkenzi in forum Free Hosting
    Replies: 1
    Last Post: 05-21-2010, 05:47 AM
  3. Formas de pago / Paying methods
    By wesker in forum General
    Replies: 6
    Last Post: 12-11-2009, 02:11 AM
  4. Best Methods To Make Money Online
    By visitor0 in forum Earning Money
    Replies: 5
    Last Post: 08-12-2009, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers