mardi 13 février 2018

Can you resolve late static bindings out of scope in PHP?

I finally found myself learning about scope and context, and the difference between them, but now I'm having problems.

I have a static method in a parent class that's inherited by a child class. When I call the method from the child, I want to be able to tell, in the parent, which child called it.

Late static bindings, right? Cool. Like this:

class Daddy {
    public static function madSkillz() {
        var_dump( static::class );
    }
}

class Sonny extends Daddy {}

Sonny::madSkillz();

And in the output, I see:

string(5) "Sonny"

Hooray, exactly what I was hoping for! BUT... now let's say that I call a function from within that parent static method. I can use debug_backtrace to find out that the static method was called, but unfortunately I see the declaring class, rather than the scope.

function GoToTheFair() {
    var_dump( debug_backtrace() );
}

class Daddy {
    public static function madSkillz() {
        var_dump( static::class );
        GoToTheFair();
    }
}

class Sonny extends Daddy {}

Sonny::madSkillz();

This prints the output:

string(5) "Sonny"
array(2) {
  [0]=>
  array(4) {
    ["file"]=>
    string(28) "/home/branja/Desktop/cry.php"
    ["line"]=>
    int(10)
    ["function"]=>
    string(11) "GoToTheFair"
    ["args"]=>
    array(0) {
    }
  }
  [1]=>
  array(6) {
    ["file"]=>
    string(28) "/home/branja/Desktop/cry.php"
    ["line"]=>
    int(16)
    ["function"]=>
    string(9) "madSkillz"
    ["class"]=>
    string(5) "Daddy"
    ["type"]=>
    string(2) "::"
    ["args"]=>
    array(0) {
    }
  }
}

I want to be able to create a ReflectionMethod object in GoToTheFair() and invoke other methods, but I need to be sure I'm using the proper scope, which should be Sonny, not Daddy.

Is there a way to do this without passing static::class as an argument to GoToTheFair()?


EDIT: The reason I do not want to use the argument approach is that the GoToTheFair() function is the library I'm coding and the classes Daddy and Sonny are the user's. I do not want them to have the freedom to choose their own scope.





Aucun commentaire:

Enregistrer un commentaire