php实现面向对象的一个显著特征是大量使用关键字,本文将详细介绍关键字
public表示公有,它具有最大的访问权限,被定义为公有的类成员可以在任何地方被访问
如果属性用 var 定义,则被视为公有,如果方法没有设置关键字,则该方法默认为公有
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> demo{ </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$public</span> = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">function</span> test(<span style="color: #800080;">$var</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "{<span style="color: #800080;">$var</span>}000"<span style="color: #000000;">; } } </span><span style="color: #800080;">$d1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> demo; </span><span style="color: #800080;">$d1</span>->test(<span style="color: #800080;">$d1</span>-><span style="color: #0000ff;">public</span>);<span style="color: #008000;">//</span><span style="color: #008000;">1000</span> ?>
protected表示受保护的,被定义为受保护的类成员则可以被其自身以及其子类和父类访问
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> demo{ </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> fn(){ </span><span style="color: #0000ff;">echo</span> '111'<span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span> demo1 <span style="color: #0000ff;">extends</span><span style="color: #000000;"> demo{ </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> test(){ parent</span>::<span style="color: #000000;">fn(); } } </span><span style="color: #800080;">$d1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> demo1; </span><span style="color: #800080;">$d1</span>->test();<span style="color: #008000;">//</span><span style="color: #008000;">111</span> ?>
private表示私有的,被定义为私有的类成员则只能被其定义所在的类访问
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> demo{ </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$private</span> = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> test(){ </span><span style="color: #0000ff;">echo</span>(<span style="color: #800080;">$this</span>-><span style="color: #0000ff;">private</span><span style="color: #000000;">); } } </span><span style="color: #800080;">$d1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> demo; </span><span style="color: #800080;">$d1</span>->test();<span style="color: #008000;">//</span><span style="color: #008000;">1</span> ?>
PHP5新增了final关键字,它只能用来修饰类和方法,不能使用final这个关键字来修饰成员属性,因为final是常量的意思,我们在PHP里定义常量使用的是define()函数和const关键字,所以不能使用final来定义成员属性
如果父类中的方法被声明为final,则子类无法覆盖该方法。如果一个类被声明为 final,则不能被继承
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> BaseClass { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test() { </span><span style="color: #0000ff;">echo</span> "BaseClass::test() called\n"<span style="color: #000000;">; } </span><span style="color: #0000ff;">final</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> moreTesting() { </span><span style="color: #0000ff;">echo</span> "BaseClass::moreTesting() called\n"<span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span> ChildClass <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseClass { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> moreTesting() { </span><span style="color: #0000ff;">echo</span> "ChildClass::moreTesting() called\n"<span style="color: #000000;">; } } </span><span style="color: #008000;">//</span><span style="color: #008000;"> Results in Fatal error: Cannot override final method BaseClass::moreTesting()</span> ?>
static关键字表示静态的意思,用于修饰类的成员属性和成员方法(即为静态属性和静态方法)
类中的静态属性和静态方法不用实例化(new)就可以直接使用类名访问
[注意]静态属性不能通过一个类已实例化的对象来访问,但静态方法可以
由于静态方法不需要通过对象即可调用,所以伪变量 $this 在静态方法中不可用,静态属性不可以由对象通过 -> 操作符来访问
用静态方式调用一个非静态方法会导致一个 E_STRICT 级别的错误
就像其它所有的 PHP 静态变量一样,静态属性只能被初始化为文字或常量,不能使用表达式。所以可以把静态属性初始化为整数或数组,但不能初始化为另一个变量或函数返回值,也不能指向一个对象
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Foo { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$my_static</span> = 'foo'<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> staticValue() { </span><span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$my_static</span><span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span> Bar <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Foo { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> fooStatic() { </span><span style="color: #0000ff;">return</span> parent::<span style="color: #800080;">$my_static</span><span style="color: #000000;">; } } </span><span style="color: #0000ff;">print</span> Foo::<span style="color: #800080;">$my_static</span> . "\n";<span style="color: #008000;">//</span><span style="color: #008000;">'foo'</span> <span style="color: #800080;">$foo</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Foo(); </span><span style="color: #0000ff;">print</span> <span style="color: #800080;">$foo</span>->staticValue() . "\n";<span style="color: #008000;">//</span><span style="color: #008000;">'foo'</span> <span style="color: #0000ff;">print</span> <span style="color: #800080;">$foo</span>::<span style="color: #800080;">$my_static</span> . "\n";<span style="color: #008000;">//</span><span style="color: #008000;">'foo'</span> <span style="color: #0000ff;">print</span> <span style="color: #800080;">$foo</span>->my_static . "\n"; <span style="color: #008000;">//</span><span style="color: #008000;">报错 </span> ?>
可以把在类中始终保持不变的值定义为常量。在定义和使用常量的时候不需要使用$符号,而是使用const
常量的值必须是一个定值,不能是变量,类属性,数学运算的结果或函数调用
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> MyClass { </span><span style="color: #0000ff;">const</span> <span style="color: #008080;">constant</span> = 'constant value'<span style="color: #000000;">; </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> showConstant() { </span><span style="color: #0000ff;">echo</span> self::<span style="color: #008080;">constant</span> . "\n"<span style="color: #000000;">; } } </span><span style="color: #0000ff;">echo</span> MyClass::<span style="color: #008080;">constant</span> . "\n";<span style="color: #008000;">//</span><span style="color: #008000;">'constant value'</span> <span style="color: #800080;">$classname</span> = "MyClass"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$classname</span>::<span style="color: #008080;">constant</span> . "\n"; <span style="color: #008000;">//</span><span style="color: #008000;">'constant value'</span> <span style="color: #800080;">$class</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass(); </span><span style="color: #800080;">$class</span>->showConstant();<span style="color: #008000;">//</span><span style="color: #008000;">'constant value'</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$class</span>::<span style="color: #008080;">constant</span>."\n";<span style="color: #008000;">//</span><span style="color: #008000;">'constant value'</span> ?>
当一个方法在类定义内部被调用时,有一个可用的伪变量this,特殊对象的引用this就是在对象内部的成员方法中,代表本对象的一个引用,但只能在对象的成员方法中使用,不管是在对象内部使用$this访问自己对象内部成员。还是在对象外部通过对象的引用名称访问对象中的成员,都需要使用特殊的运算符“->”来完成访问
[注意]this在静态方法中不可用
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> A { </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> foo() { </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">echo</span> '$this is defined ('<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">get_class</span>(<span style="color: #800080;">$this</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> ")\n"<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">echo</span> "\$this is not defined.\n"<span style="color: #000000;">; } } } </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> B { </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> bar() { </span><span style="color: #008000;">//</span><span style="color: #008000;"> Note: the next line will issue a warning if E_STRICT is enabled.</span> A::<span style="color: #000000;">foo(); } } </span><span style="color: #800080;">$a</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> A(); </span><span style="color: #800080;">$a</span>->foo();<span style="color: #008000;">//</span><span style="color: #008000;">$this is defined (A) </span> A::foo();<span style="color: #008000;">//</span><span style="color: #008000;">$this is not defined. </span> <span style="color: #800080;">$b</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> B(); </span><span style="color: #800080;">$b</span>->bar();<span style="color: #008000;">//</span><span style="color: #008000;">$this is defined (B) </span> B::bar();<span style="color: #008000;">//</span><span style="color: #008000;">$this is not defined.</span> ?>
在类的方法中,不能用this来引用静态变量或静态方法,而需要用self来引用
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> MyClass { </span><span style="color: #0000ff;">const</span> <span style="color: #008080;">constant</span> = 'constant value'<span style="color: #000000;">; </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> showConstant() { </span><span style="color: #0000ff;">echo</span> self::<span style="color: #008080;">constant</span> . "\n"<span style="color: #000000;">; } } </span><span style="color: #800080;">$var</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$var</span>->showConstant();<span style="color: #008000;">//</span><span style="color: #008000;">constant value</span> ?>
parent用于调用父类中定义的成员方法或常量
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> MyClass{ </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> fn(){ </span><span style="color: #0000ff;">echo</span>('111'<span style="color: #000000;">); } </span><span style="color: #0000ff;">const</span> A = 'a'<span style="color: #000000;">; } </span><span style="color: #0000ff;">class</span> Class1 <span style="color: #0000ff;">extends</span><span style="color: #000000;"> MyClass{ </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> test(){ </span><span style="color: #0000ff;">echo</span> parent::fn().parent::<span style="color: #000000;">A; } } </span><span style="color: #800080;">$var</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Class1; </span><span style="color: #800080;">$var</span>->test();<span style="color: #008000;">//</span><span style="color: #008000;">111a</span> ?>