SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast dgx's Avatar
    Join Date
    Aug 2005
    Location
    The Czech Republic
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    self::Bar() vs. static::Bar() workaround

    Inheritance with the static elements is a nightmare in PHP5. 'self::' refers to the class where method is declared, not the actual class which is called at runtime.

    PHP Code:
    class Base
    {
       static function 
    Foo()
       {
           
    self::Bar();
       }


       static function 
    Bar()
       {
           echo 
    "Base::Bar()";
       }
       
    }

    class 
    Derived extends Base
    {
       static function 
    Bar ()
       {
           echo 
    "Derived::Bar()";
       }
    }

    Derived::Foo(); // we want this to print "Derived::Bar()", but it prints Base::Bar() 
    PHP6 should solve this using 'static::Bar()' instead of 'self::Bar()'.

    The another problem - $varClass::Bar() leads to fatal error.


    The workaround:

    PHP Code:

    // replace Class::method() with _call("Class::method") 
    // and PHP6 static::method() emulate as _call("$static::method()")

    class Base
    {
       static function 
    Foo()
       {
           
    $static $GLOBALS['!static'];
           
           
    // use $static:: instead of static::
           
    _call("$static::Bar");
       }


       static function 
    Bar()
       {
           echo 
    "Base::Bar()";
       }
       
    }

    class 
    Derived extends Base
    {
       static function 
    Bar ()
       {
           echo 
    "Derived::Bar()";
       }
    }


    _call("Derived::Foo"); // prints Derived::Bar(), WOW!

    // or:
    $class 'Derived';
    _call("$class::Foo");

    // with arguments
    _call("Deriver::Foo", array('some''arguments'));
            

    function 
    _call($class__method$args null) {
        return 
    call_user_func_array(
               list(
    $GLOBALS['!static'], ) = explode('::'$class__method2),
               
    $args
        
    );

    The same problem with class constants:

    PHP Code:
    class Base
    {
       const 
    bar 'Base';

       static function 
    Foo()
       {
           echo 
    self::bar;
       }
       
    }

    class 
    Derived extends Base
    {
       const 
    bar 'Derived';

    }

    Derived::Foo(); // we want this to print "Derived", but prints "Base" 
    The workaround (nearly same princip):

    PHP Code:
    class Base
    {
       const 
    bar 'Base';

       static function 
    Foo()
       {
           
    $static $GLOBALS['!static'];

           echo 
    constant("$static::bar");
       }
       
    }

    class 
    Derived extends Base
    {
       const 
    bar 'Derived';

    }


    _call("Derived::Foo"); // prints Derived !

    // or

    $class 'Derived';
    echo 
    constant("$class::bar");

            
    function 
    _call($class__method$args null) {
        return 
    call_user_func_array(
               list(
    $GLOBALS['!static'], ) = explode('::'$class__method2),
               
    $args
        
    );

    Not too elegant, but it works...
    Last edited by dgx; Dec 12, 2005 at 23:28.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What makes you think it works?

  3. #3
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regarding static method calls, this behaviors are explained in the manual:

    In fact static method calls are resolved at compile time. When using an explicit class name the method is already identified completely and no inheritance rules apply. If the call is done by self then self is translated to the current class, that is the class the code belongs to. Here also no inheritance rules apply.
    As for changing the value of a constant in a subclass, I'm not sure if this is permitted in any language... but it is confusing that it doesn't throw an error, and simply doesn't work

  4. #4
    SitePoint Enthusiast dgx's Avatar
    Join Date
    Aug 2005
    Location
    The Czech Republic
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    What makes you think it works?
    Are there any bugs? I think it works well.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •