我想知道是否有任何方法可以防止在PHP中的任何类上下文中使用特征方法?
让我用一个简短的例子来解释我想要的东西,这是我目前的代码:
// File : MyFunctions.php
trait MyFunctions {
function hello_world() {
echo 'Hello World !';
}
}
// File : A.php
include 'MyFunctions.php';
class A {
use MyFunctions;
}
// File : testTraits.php
include 'A.php';
hello_world(); // Call to undefined function -> OK, expected
A::hello_world(); // Hello World ! -> OK, expected
MyFunctions::hello_world(); // Hello World ! -> Maybe OK, but not expected, I'd like to prevent it
Run Code Online (Sandbox Code Playgroud)
关于特征的PHP手册页非常全面,很多案例都得到了处理,但不是这个(http://php.net/manual/en/language.oop5.traits.php)
我绝望地试图删除"静态"并使用"公共","受保护","私人",但当然,它只是没有用.到目前为止我还没有其他想法,所以也许我错过了什么,或者这只是不可能的?
使用特征时使用可见性更改功能:
trait MyFunctions {
private function _hello_world() {
echo 'Hello World !';
}
}
class A {
use MyFunctions { _hello_world as public hello_world ;}
...
}
Run Code Online (Sandbox Code Playgroud)