Assuming I have the following classes in different files:
<?php
namespace MyNS;
class superclass {
public function getNamespace(){
return __NAMESPACE__;
}
}
?>
<?php
namespace MyNSSubNS;
class childclass extends superclass { }
?>
If I instantiate "childclass" and call getNamespace() it returns "MyNS".
Is there any way to get the current namespace from the child class without redeclaring the method?
I've resorted to creating a static $namespace variable in each class and referencing it using super::$namespace
but that just doesn't feel very elegant.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…