是否有必要在模型中使用私有函数?

是否有必要在模型中使用私有函数?

问题描述:

I'm making a model in CodeIgniter and I don't want any of the functions to be accessible to the user. For controllers you would do something like:

private function _myfunction()
{
    dosomething();
}

I know this will also work for models but my question is, is this needed? I don't think users can initiate these functions via the URL. The reason I'm asking is because I'd like to follow best practice but if possible I'd also like to avoid prefixing everything with '_' when I call it.

我在CodeIgniter中创建模型,我不希望用户可以访问任何函数 。 对于控制器,您可以执行以下操作: p>

  private function _myfunction()
 {
 
dosomething(); 
} 
  code>  pre> \  n 
 

我知道这也适用于模型,但我的问题是,这需要吗? 我不认为用户可以通过URL启动这些功能。 我问的原因是因为我想遵循最佳实践,但如果可能的话,我也希望在调用它时避免使用'_'作为前缀。 p> div>

No, that is not necessary. It's an unusual situation for a user to have access to a class method through URL, and it's only implemented in codeigniter for controllers.

It's not necessary, and in fact prefixing private objects with "_" is depreciated with the actual use of the "private" and "protected" keywords in PHP 5+.

No its not necessary. To prevent access to the method from the browser, just add an underscore before the method: _method() . This is Ideal if you want to use/call it as a module(HMVC).