PHP Limitation: Static Inheritance and Scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wwight

    #1

    PHP Limitation: Static Inheritance and Scope

    As many people have noticed by now, PHP exhibits some frustrating
    behavior when it comes to static fields and methods. For instance,
    when a static method is defined in a parent class, but called from a
    child class, the method uses the parent class for scope (not the
    originating child class). For example, see the following block of
    code:

    class Parent {
    private static $className = 'Parent';

    public static function getName() {
    return self::$classNam e . "\n";
    }
    }

    class Child extends Parent {
    private static $className = 'Child';
    }

    echo Parent::getName ();
    echo Child::getName( );

    ------------------------------
    Intuitive Results
    ------------------------------
    Parent
    Child

    ------------------------------
    Actual Results
    ------------------------------
    Parent
    Parent

    Does anyone know if the PHP dev team has plans to remedy this issue? I
    personally feel that the inability to handle static scoping with
    inheritance is a major limitation to the OO functionality of PHP. I've
    seen several relevant bug reports on bugs.php.net, but most have been
    categorized as Bogus. One member of the dev team flat out insulted one
    of the bug reporters, saying "Sorry, you have a complete wrong idea og
    OO programming. [sic]"






    However, reading through the various bug reports, I wonder if the dev
    team may be softening just a bit in their resistance more recently. I
    recommend voting for the bugs above, submitting more functionaity
    requests, and continuing discussion in developer forums. Short of
    fixing it ourselves, what else can we do to convince the PHP dev team
    to address this problem?

  • Jerry Stuckle

    #2
    Re: PHP Limitation: Static Inheritance and Scope

    wwight wrote:[color=blue]
    > As many people have noticed by now, PHP exhibits some frustrating
    > behavior when it comes to static fields and methods. For instance,
    > when a static method is defined in a parent class, but called from a
    > child class, the method uses the parent class for scope (not the
    > originating child class). For example, see the following block of
    > code:
    >
    > class Parent {
    > private static $className = 'Parent';
    >
    > public static function getName() {
    > return self::$classNam e . "\n";
    > }
    > }
    >
    > class Child extends Parent {
    > private static $className = 'Child';
    > }
    >
    > echo Parent::getName ();
    > echo Child::getName( );
    >
    > ------------------------------
    > Intuitive Results
    > ------------------------------
    > Parent
    > Child
    >
    > ------------------------------
    > Actual Results
    > ------------------------------
    > Parent
    > Parent
    >
    > Does anyone know if the PHP dev team has plans to remedy this issue? I
    > personally feel that the inability to handle static scoping with
    > inheritance is a major limitation to the OO functionality of PHP. I've
    > seen several relevant bug reports on bugs.php.net, but most have been
    > categorized as Bogus. One member of the dev team flat out insulted one
    > of the bug reporters, saying "Sorry, you have a complete wrong idea og
    > OO programming. [sic]"
    >
    > http://bugs.php.net/bug.php?id=30140
    > http://bugs.php.net/bug.php?id=30423
    > http://bugs.php.net/bug.php?id=30934
    > http://bugs.php.net/bug.php?id=30235
    >
    > However, reading through the various bug reports, I wonder if the dev
    > team may be softening just a bit in their resistance more recently. I
    > recommend voting for the bugs above, submitting more functionaity
    > requests, and continuing discussion in developer forums. Short of
    > fixing it ourselves, what else can we do to convince the PHP dev team
    > to address this problem?
    >[/color]

    In this case I agree with the PHP developers.

    The parent class knows nothing about the child class; in fact, there may
    not even be a child class. The parent class's methods need to use only
    what's available in the parent class.

    As an example - C++ exhibits this same behavior. The C++ spec has added
    some features for dynamic typing of classes, but the static typing works
    this same way.

    If your design has the parent class depending on something in the child
    class, then your design is broken. The child class should always extend
    the parent class or make a more specific instance of a parent class, not
    vice versa.

    In your case, if you want the function to be defined in the scope of the
    child class, you need to specify the function in the child class.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Oli Filth

      #3
      Re: PHP Limitation: Static Inheritance and Scope

      wwight wrote:[color=blue]
      > As many people have noticed by now, PHP exhibits some frustrating
      > behavior when it comes to static fields and methods. For instance,
      > when a static method is defined in a parent class, but called from a
      > child class, the method uses the parent class for scope (not the
      > originating child class). For example, see the following block of
      > code:
      >
      > class Parent {
      > private static $className = 'Parent';
      >
      > public static function getName() {
      > return self::$classNam e . "\n";
      > }
      > }
      >
      > class Child extends Parent {
      > private static $className = 'Child';
      > }
      >
      > echo Parent::getName ();
      > echo Child::getName( );
      >
      > ------------------------------
      > Intuitive Results
      > ------------------------------
      > Parent
      > Child
      >
      > ------------------------------
      > Actual Results
      > ------------------------------
      > Parent
      > Parent
      >
      > Does anyone know if the PHP dev team has plans to remedy this issue?[/color]

      There is no issue to resolve. See below...
      [color=blue]
      > I personally feel that the inability to handle static scoping with
      > inheritance is a major limitation to the OO functionality of PHP. I've
      > seen several relevant bug reports on bugs.php.net, but most have been
      > categorized as Bogus. One member of the dev team flat out insulted one
      > of the bug reporters, saying "Sorry, you have a complete wrong idea og
      > OO programming. [sic]"[/color]

      They are correct!

      The same occurs in Java and C++.

      By definition, static methods are given no "this" pointer. So when you
      call Child::getName( ), it resolves to a call to Parent::getName (). The
      only scope that Parent::getName () has is the static members of Parent.

      --
      Oli

      Comment

      • Oli Filth

        #4
        Re: PHP Limitation: Static Inheritance and Scope

        wwight wrote:[color=blue]
        > As many people have noticed by now, PHP exhibits some frustrating
        > behavior when it comes to static fields and methods. For instance,
        > when a static method is defined in a parent class, but called from a
        > child class, the method uses the parent class for scope (not the
        > originating child class). For example, see the following block of
        > code:
        >
        > class Parent {
        > private static $className = 'Parent';
        >
        > public static function getName() {
        > return self::$classNam e . "\n";
        > }
        > }
        >
        > class Child extends Parent {
        > private static $className = 'Child';
        > }
        >
        > echo Parent::getName ();
        > echo Child::getName( );
        >
        > ------------------------------
        > Intuitive Results
        > ------------------------------
        > Parent
        > Child
        >
        > ------------------------------
        > Actual Results
        > ------------------------------
        > Parent
        > Parent
        >
        > Does anyone know if the PHP dev team has plans to remedy this issue?[/color]

        There is no issue to resolve. See below...
        [color=blue]
        > I personally feel that the inability to handle static scoping with
        > inheritance is a major limitation to the OO functionality of PHP. I've
        > seen several relevant bug reports on bugs.php.net, but most have been
        > categorized as Bogus. One member of the dev team flat out insulted one
        > of the bug reporters, saying "Sorry, you have a complete wrong idea og
        > OO programming. [sic]"[/color]

        They are correct!

        The same occurs in Java and C++.

        By definition, static methods are given no "this" pointer. So when you
        call Child::getName( ), it resolves to a call to Parent::getName (). The
        only scope that Parent::getName () has is the static members of Parent.

        The sort of behaviour that you expect is not static.

        --
        Oli

        Comment

        Working...