如何防止在PHP中使用特征方法超出“使用”范围
发布时间:2020-12-13 21:51:57 所属栏目:PHP教程 来源:网络整理
导读:我想知道是否有任何方法可以防止在 PHP中的任何类上下文中使用特征方法? 让我用一个简短的例子来解释我想要的东西,这是我目前的代码: // File : MyFunctions.phptrait MyFunctions { function hello_world() { echo 'Hello World !'; }}// File : A.phpinc
我想知道是否有任何方法可以防止在
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 关于特征的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 ;} ... } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |