php CodeIgniter 中的公共函数与函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9849984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:46:35  来源:igfitidea点击:

Public functions vs Functions in CodeIgniter

phpfunctioncodeignitermethods

提问by Derfder

In PHP, What is the difference between declaring methods inside class like

在 PHP 中,在类中声明方法有什么区别

public functionVS function

public functionVS 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 functionor functionand why?

使用公共函数函数是更好的做法,为什么?

采纳答案by sas

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

回答by Joseph

According to PHP.net

根据PHP.net

Class methods may be defined as public, private, or protected. Methods declared without any explicit visibility keyword are defined as public.

类方法可以定义为 public、private 或 protected。没有任何显式可见性关键字声明的方法被定义为 public

for best practice, i suggest using visibility keywords (esp when using higher versions of PHP). it prevents confusion (like the one you are in now) and promotes standard practice in coding.

对于最佳实践,我建议使用可见性关键字(尤其是在使用更高版本的 PHP 时)。它可以防止混淆(就像你现在所处的那样)并促进编码的标准实践。

回答by romoti

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()否则不会被调用

回答by Manse

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

这里文档

回答by Bananenspin

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. 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.

如果您真的想要最佳实践,您将始终使用 public。但是对于 codeigniter 框架,无论您是否将其声明为 public 都无关紧要。请注意,如果您希望控制器是私有的,则不要使用私有,而是在控制器名称前使用下划线 (_),这样它就不会可见。

回答by jerrytom

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