关闭

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

913人阅读 评论(0) 收藏 举报

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)

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7711541次
    • 积分:152108
    • 等级:
    • 排名:第3名
    • 原创:6348篇
    • 转载:35篇
    • 译文:2篇
    • 评论:2710条
    文章分类
    最新评论
    热门站