2013/1/14 Alexander Lissachenko <lisachenko...@gmail.com>

> Hi! It's my first letter here )
>
> I want to suggest a small improvement for ReflectionMethod->invoke()
> and ReflectionMethod->invokeArgs() methods to support LSB for
> overridden static methods. Currently, for invoking static methods
> first argument should be null, information about class (scope) is
> taken from the reflection class. However, there is one issue that can
> not be solved at the current time.
>
> Suppose, we have two classes:
>
> class First
> {
>     public static function foo()
>     {
>         echo get_called_class();
>     }
> }
>
> class Second extends First
> {
>     public static function foo()
>     {
>         echo "Do not call me, please";
>     }
> }
>
> Now I want to invoke the First::foo() method with Reflection from the
> Second class scope for using LSB. Currently this is impossible:
> $class = new ReflectionClass('First');
> $class->getMethod('foo')->invokeArgs(null, array()); // Outputs
> 'First' as no scope information is passed


> $class = new ReflectionClass('Second');
> $class->getMethod('foo')->invokeArgs(null, array()); // Outputs 'Do
> not call me, please' as method is redefined
>
> So, there is no way now to invoke the static First::foo() method from
> the child scope because it was redefined. However, this can be easily
> implemented by adding the scope for static methods invocation (like
> Closure::bindTo()):
>
> $class = new ReflectionClass('First');
> $class->getMethod('foo')->invokeArgs('Second', array()); // Outputs
> 'Second'
>
> This improvement can be very useful for building proxies for static
> methods, that use LSB. Can it be implemented for PHP 5.3-5.5? Thanks!
>

Maybe it's just me, but could you explain which use-case want to solve? The
example isn't very useful, because you can achieve this quite easy without
reflection. Also why do you override the method, when you don't want it to
get called? Shouldn't they two separate methods then?

<?php
class First
{
    public static function foo()
    {
        echo get_called_class();
    }
}

class Second extends First
{
    public static function foo()
    {
        echo "Do not call me, please";
    }
public static function bar() {
parent::foo();
}
}
Second::bar();

http://codepad.viper-7.com/fwG5GB


>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
github.com/KingCrunch

Reply via email to