[PATCH] Late Static Binding

From: Date: Thu, 23 Feb 2006 22:06:02 +0000
Subject: [PATCH] Late Static Binding
Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
I have finished a patch that implements a working version of late static
binding.

I used the notes from the Paris PDM as my guidelines for implementation.
(http://www.php.net/~derick/meeting-notes.html#late-static-binding-using-this-without-or-perhaps-with-a-different-name)
As requested in the notes I reused 'static::'. I also wrote a few tests not only to test
the new functionality but also to make sure 'static' can't be inherited or extended. 

I also added a new function get_caller_class() which returns the name of
the class that static:: would represent.

(borrowing from PDM notes)
In php5.* the following script outputs "A::static2":

<?php
        class A {
                static function staticA() {
                        self::static2();
                }

                static function static2() {
                        echo "A::static2\n";
                }
        }

        class B extends A {
                static function static2() {
                        echo "B::static2\n";
                }
        }

        B::staticA();
?>

This has somewhat recently been highlighted by different developers to
be somewhat problematic behavior in creating user friendly APIs. If you
want to see a possible use for it you need look no further than the
example ZActiveRecord API that was used in their webcast with php|arch.
(http://blog.joshuaeichorn.com/archives/2006/01/09/zactiverecord-cant-work/) Currently the code laid
out there is impossible short of some ugly use of debug_backtrace() and file parsing :/. This patch
of course would allow that kind of code too exist.

In a small example based on the one I gave earlier you could change the
code too the following and have it print "B::static2":

<?php
        class A {
                static function staticA() {
                        static::static2();
                }

                static function static2() {
                        echo "A::static2\n";
                }
        }

        class B extends A {
                static function static2() {
                        echo "B::static2\n";
                }
        }

        B::staticA();
?>

As far as current userland code impact, there is very little as far as I
can tell. No keywords have been added, just another use for an existing
one. No changes were made to self:: or parent:: so the change should be
pretty transparent. The only thing that I see remotely causing any
issues would be the new function (get_caller_class().) I added that just
to complete the set so to speak.

----------
Mike Lively (ds- on irc.efnet.org)
"Our Schwartz is bigger than yours"


Attachment: [text/x-patch] late-static-binding.patch

Thread (59 messages)

« previous php.internals (#21991) next »