前端學PHP之物件導向系列第四篇-----關鍵字

來源:互聯網
上載者:User

標籤:靜態屬性   base   toolbar   result   初始化   物件導向   cal   函數調用   表達   

public

  public表示公有,它具有最大的存取權限,被定義為公有的類成員可以在任何地方被訪問

  如果屬性用 var 定義,則被視為公有,如果方法沒有設定關鍵字,則該方法預設為公有

<?phpclass demo{    public $public = 1;    function test($var){        echo "{$var}000";    }}$d1 = new demo;$d1->test($d1->public);//1000?>

 

protected

  protected表示受保護的,被定義為受保護的類成員則可以被其子類和父類訪問

<?phpclass demo{    protected function fn(){        echo ‘111‘;    }}class demo1 extends demo{    function test(){        parent::fn();    }    }$d1 = new demo1;$d1->test();//111?>

 

private

  private表示私人的,被定義為私人的類成員則只能被其定義所在的類訪問

<?phpclass demo{    private $private = 1;    function test(){        echo($this->private);    }}$d1 = new demo;$d1->test();//1?>    

 

final

  PHP5新增了final關鍵字,它只能用來修飾類和方法,不能使用final這個關鍵字來修飾成員屬性,因為final是常量的意思,我們在PHP裡定義常量使用的是define()函數和const關鍵字,所以不能使用final來定義成員屬性

  如果父類中的方法被聲明為final,則子類無法覆蓋該方法。如果一個類被聲明為 final,則不能被繼承

<?phpclass BaseClass {   public function test() {       echo "BaseClass::test() called\n";   }   final public function moreTesting() {       echo "BaseClass::moreTesting() called\n";   }}class ChildClass extends BaseClass {   public function moreTesting() {       echo "ChildClass::moreTesting() called\n";   }}// Results in Fatal error: Cannot override final method BaseClass::moreTesting()?>

 

static

  static關鍵字表示靜態意思,用於修飾類的成員屬性和成員方法(即為靜態屬性和靜態方法)

  類中的靜態屬性和靜態方法不用執行個體化(new)就可以直接使用類名訪問

  [注意]靜態屬性不能通過一個類已執行個體化的對象來訪問,但靜態方法可以

  由於靜態方法不需要通過對象即可調用,所以偽變數 $this 在靜態方法中不可用,靜態屬性不可以由對象通過 -> 操作符來訪問

  用靜態方式調用一個非靜態方法會導致一個 E_STRICT 層級的錯誤

  就像其它所有的 PHP 靜態變數一樣,靜態屬性只能被初始化為文字或常量,不能使用運算式。所以可以把靜態屬性初始化為整數或數組,但不能初始化為另一個變數或函數傳回值,也不能指向一個對象

<?phpclass Foo{    public static $my_static = ‘foo‘;    public function staticValue() {        return self::$my_static;    }}class Bar extends Foo{    public function fooStatic() {        return parent::$my_static;    }}print Foo::$my_static . "\n";//‘foo‘$foo = new Foo();print $foo->staticValue() . "\n";//‘foo‘print $foo::$my_static . "\n";//‘foo‘print $foo->my_static . "\n"; //報錯 ?>

 

const

  可以把在類中始終保持不變的值定義為常量。在定義和使用常量的時候不需要使用$符號,而是使用const

  常量的值必須是一個定值,不能是變數,類屬性,數學運算的結果或函數調用

<?phpclass MyClass{    const constant = ‘constant value‘;    function showConstant() {        echo  self::constant . "\n";    }}echo MyClass::constant . "\n";//‘constant value‘$classname = "MyClass";echo $classname::constant . "\n"; //‘constant value‘$class = new MyClass();$class->showConstant();//‘constant value‘echo $class::constant."\n";//‘constant value‘?>

 

this

  當一個方法在類定義內部被調用時,有一個可用的偽變數this,特殊對象的引用this就是在對象內部的成員方法中,代表本對象的一個引用,但只能在對象的成員方法中使用,不管是在對象內部使用$this訪問自己對象內部成員。還是在對象外部通過對象的引用名稱訪問對象中的成員,都需要使用特殊的運算子“->”來完成訪問

  [注意]this在靜態方法中不可用

<?phpclass A{    function foo()    {        if (isset($this)) {            echo ‘$this is defined (‘;            echo get_class($this);            echo ")\n";        } else {            echo "\$this is not defined.\n";        }    }}class B{    function bar()    {        // Note: the next line will issue a warning if E_STRICT is enabled.        A::foo();    }}$a = new A();$a->foo();//$this is defined (A) A::foo();//$this is not defined. $b = new B();$b->bar();//$this is defined (B) B::bar();//$this is not defined.?>

 

self

  在類的方法中,不能用this來引用靜態變數或靜態方法,而需要用self來引用

<?phpclass MyClass{    const constant = ‘constant value‘;    static function showConstant() {        echo  self::constant . "\n";    }}$var = new MyClass;echo $var->showConstant();//constant value?>

 

parent

  parent用於在子類中調用父類中定義的成員方法或常量

<?phpclass MyClass{    function fn(){        echo(‘111‘);    }    const A = ‘a‘;}class Class1 extends MyClass{    function test(){        echo parent::fn().parent::A;    }}$var = new Class1;$var->test();//111a?>
 

前端學PHP之物件導向系列第四篇-----關鍵字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.