[英]Public functions vs Functions in CodeIgniter
In PHP, What is the difference between declaring methods inside class like 在PHP中,在类中声明方法之间有什么区别
public function
VS function
public function
VS function
For example: 例如:
public function contact()
{
$data['header'] = "Contact";
$this->load->view('admin/admin_contact', $data);
}
VS VS
function contact()
{
$data['header'] = "Contact";
$this->load->view('admin/admin_contact', $data);
}
Is it better practice to use public function or function and why? 使用公共功能或功能是更好的做法,为什么?
According to PHP.net 根据PHP.net
Class methods may be defined as public, private, or protected. 类方法可以定义为public,private或protected。 Methods declared without any explicit visibility keyword are defined as public . 声明没有任何显式可见性关键字的方法被定义为public 。
for best practice, i suggest using visibility keywords (esp when using higher versions of PHP). 为了获得最佳实践,我建议使用可见性关键字(尤其是在使用更高版本的PHP时)。 it prevents confusion (like the one you are in now) and promotes standard practice in coding. 它可以防止混淆(就像你现在的那样)并促进编码的标准做法。
Methods declared with any explicit visibility keyword is best practice. 使用任何显式可见性关键字声明的方法是最佳实践 It looks and feels better and it doesn't confuse people. 它看起来和感觉更好,它不会混淆人们。
There is no difference between these two. 这两者没有区别。 Both are the same. 两者都是一样的。 In codeigniter both have same meaning and can be called by using standard URI tags unless you give a '_' in front of your function name _fname()
will not be called 在codeigniter中,两者具有相同的含义,并且可以使用标准URI标记进行调用,除非在函数名称前面给出“_”,不会调用_fname()
If you really want best practice you will always use public. 如果你真的想要最佳实践,你将永远使用公共。 But for the codeigniter Framework it doesn't mather if you declare it public or not. 但对于codeigniter框架,如果你声明它是公开的,它就不会发生。 Note that if you want a controller to be private you dont use private but you will use the underscore (_) in front of your controller name so it wont be visible. 请注意,如果您希望控制器是私有的,则不要使用私有,但您将在控制器名称前使用下划线(_),以使其不可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.