PHP中类方法的默认可见性 [英] Default visibility of class methods in PHP

查看:46
本文介绍了PHP中类方法的默认可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了手册,但我似乎看不到找到答案.

I looked at the manual, but I can't seem to find the answer.

没有可见性声明的方法在PHP中的默认可见性是什么? PHP是否像Java一样具有包可见性?

What is the default visibility in PHP for methods without a visibility declaration? Does PHP have a package visibility like in Java?

例如,在下面的代码中,go()是公开的还是私有的?

For example, in the following code, is go() public or private?

class test {
  function go() {
  }
}

我问的原因是,我已经看到很多构造函数代码写为function __construct(),有些写为public function __construct().它们等效吗?

The reason I asked is that I've seen many constructors code written as function __construct() and some as public function __construct(). Are they equivalent?

推荐答案

默认为公开.

类方法可以定义为公共,私有或受保护的.没有任何显式可见性关键字声明的方法定义为public.

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

http://www.php.net/manual/zh/language.oop5.visibility.php

这篇关于PHP中类方法的默认可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆