简体   繁体   English

公共函数与CodeIgniter中的函数

[英]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. 它看起来和感觉更好,它不会混淆人们。

  • Most PHP5 coding conventions (eg Zend, Symfony...) require the public keyword, so it's familiar. 大多数PHP5编码约定(例如Zend,Symfony ......)都需要public关键字,所以它很熟悉。
  • It means that variable and method declarations use the same syntax. 这意味着变量和方法声明使用相同的语法。
  • It's more explicit and forces developers to consider their method visibility. 它更明确,并迫使开发人员考虑他们的方法可见性。

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()

They are the same thing .... if you do not specify the visibility methods / functions are declared as public 它们是一样的....如果你没有指定可见性方法/函数被声明为public

Methods declared without any explicit visibility keyword are defined as public 声明没有任何显式可见性关键字的方法被定义为public

from the docs here 来自这里文档

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. 请注意,如果您希望控制器是私有的,则不要使用私有,但您将在控制器名称前使用下划线(_),以使其不可见。

  • Both declarations are same and both functions will be available by URI request in codeigniter 两个声明都是相同的,并且两个函数都可以通过codeigniter中的URI请求获得
  • To prevent a method from being called by user use private or protected access specifiers. 要防止用户调用方法,请使用私有或受保护的访问说明符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM