CakePHP method visibility
I am a little bit confused right now...in the CakePHP documentation, it states that PHP's visibility can be replaced as follows: private
with __
and protected
with _
. But doing the necessary replacements, and calling a private
method from 开发者_如何学Cwithin another class leads to the execution of that method without any restriction. What am I missing?
If you're setting them in the model, just use private and protected, the _ and __ only work for controller actions
the underscore is only effective for controller method, since the user can't access it. Inside Cake app, it's really just a convention. Besides I don't think it's a problem: You only need to lock your house from outside, you don't need to lock every doors if you are the only one in it. If you want to achieve that use private
and protected
PHP keywords.
Using _ and __ for protected resp. private methods is a CakePHP convention because CakePHP (up to v1.3.x) is still a PHP4 framework and PHP4 doesn't have the protected
and private
keywords. This means, that even if you prefix your method names in such a way, they are still public methods as far as PHP is concerned.
In your application I would follow the approach used by the coming CakePHP 2: use the naming convention together with the respective visibility keyword. For example, a private method would then be defined as private function __myPrivateMethod()
.
精彩评论