PHP:属性可见性、静态等 [英] PHP: Property visibilty, static, etc
本文介绍了PHP:属性可见性、静态等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这是一个非常愚蠢的问题,但我并没有真正理解一些东西,我每天都在使用它们,但我不确定它们是如何工作的.
I know that this is a real stupid question, but I don't really understand something, I use them everyday, but I'm not sure how they work.
class Bootstrap
{
public static $frontController = null;
public static $root = '';
公共静态、受保护、公共...所有这些之间的区别是什么?什么时候最好使用每一个...
What's the diff between public static, protected, public...all of them? and when it's best to use each of one...
最好的问候
推荐答案
static
表示该值是通过self::$var
而不是$this->var
访问的,不是特定于实例的(即它也可用于静态方法),因此非常适合单例和类似模式- a
public
var 可以从任何地方访问,即从类内部和外部 protected
var 只能从类内部以及从定义 var 的类继承的类访问- a
private
var 只能从类内部访问 static
means the value is accessed viaself::$var
instead of$this->var
, is not instance-specific (i.e. it's also available in static methods) and thus ideal for singletons and similar patterns- a
public
var is accessible from everywhere, i.e. both from inside the class and outside - a
protected
var is only accessible from inside the class and from classes inheriting from the class where the var is defined - a
private
var is only accessible from inside the class abstract
类不能被实例化,只能作为基类使用;包含抽象方法的类必须是抽象的,从抽象类继承的类必须是抽象的,除非基类的所有抽象方法都实际实现了final
类不能继承- an
abstract
class cannot be instantiated but only used as a base class; a class containing abstract methods must be abstract and a class inheriting from an abstract class must be abstract unless all abstract methods of the base class are actually implemented - a
final
class cannot be inherited from
既然您问的是 OOP 基础知识,这里还有一些值得解释的关键字:
Since you are asking about OOP basics, here are some more keywords worth explaining:
这篇关于PHP:属性可见性、静态等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文