Late static binding....sorta :/ - Digital Sandwich - PHP

来源:互联网 发布:township淘宝绿钞原理 编辑:程序博客网 时间:2024/06/17 05:17

The good news is late static binding has been introduced into head and looks like it will be merged into 5.3 before it is released. The horrible news is I really don't think the patch went as far as it needs to.

If you look at the original posts that cropped up about a year and a half ago the whole purpose of late static binding was to allow the same kind of flexibility provided by inheritance of standard class methods for static methods, properties, and constants. This wouldn't really open the door for any grandios, new kind of applications, it would just allow a new way to code libraries the most prominant example being an Active Record Library.

This is now possible, however I think there is a very unfortunate limitation that I brought up a few times on the Internals mailing list to apparently no avail. The problem is with the fact that static will ALWAYS return the 'resolved' name of the class used to call the current function. So, imagine the following method:


class Foo
{
        //...

        static public function test()
        {
                return static::$some_property;
        }

        //...
}

class Bar extends Foo
{
}
 

If you call test using Foo::test() then static:: will resolve to the 'Foo' class. If you call it using Bar::test() then static:: will resolve to 'Bar'. This is correct and works well for simple inheritance. However things start taking a downward turn the more you use inheritance. Consider the following change to Bar and the addition of a new class Bar_Child:


class Foo
{
        //...

        static public function test()
        {
                return static::$some_property;
        }

        //...
}

class Bar extends Foo
{
        static public function test()
        {
                // Do some work specific to Bar

                return parent::test();
        }

Truncated by Planet PHP, read more at the original (another 1921 bytes)

 
原创粉丝点击