PHP新手之學習類與對象(1)_PHP教程

來源:互聯網
上載者:User
PHP 5 引入了新的物件模型(Object Model)。完全重寫了 PHP 處理對象的方式,允許更佳效能和更多特性。

一、基本概念

1、class

每個類的定義都以關鍵字 class 開頭,後面跟著類名,可以是任何非 PHP 保留字的名字。後面跟著一對花括弧,裡麵包含有類成員和方法的定義。偽變數 $this 可以在當一個方法在對象內部調用時使用。$this 是一個到調用對象(通常是方法所屬於的對象,但也可以是另一個對象,如果該方法是從第二個對象內靜態調用的話)的引用。看下面例子:

Example#1 物件導向語言中的 $this 變數

 
  1. class A
  2. {
  3. function foo()
  4. {
  5. if (isset($this)) {
  6. echo '$this is defined (';
  7. echo get_class($this);
  8. echo ")n";
  9. } else {
  10. echo "$this is not defined.n";
  11. }
  12. }
  13. }
  14. class B
  15. {
  16. function bar()
  17. {
  18. A::foo();
  19. }
  20. }
  21. $a = new A();
  22. $a->foo();
  23. A::foo();
  24. $b = new B();
  25. $b->bar();
  26. B::bar();
  27. ?>

上例將輸出:

 
  1. $this is defined (a)
  2. $this is not defined.
  3. $this is defined (b)
  4. $this is not defined.

Example#2 簡單的類定義

 
  1. class SimpleClass
  2. {
  3. // 成員聲明
  4. public $var = 'a default value';
  5. // 方法聲明
  6. public function displayVar() {
  7. echo $this->var;
  8. }
  9. }
  10. ?>

Example#3 類成員的預設值

 
  1. class SimpleClass
  2. {
  3. // 無效的類成員定義:
  4. public $var1 = 'hello '.'world';
  5. public $var2 = <<
  6. hello world
  7. EOD;
  8. public $var3 = 1+2;
  9. public $var4 = self::myStaticMethod();
  10. public $var5 = $myVar;
  11. // 正確的類成員定義:
  12. public $var6 = myConstant;
  13. public $var7 = self::classConstant;
  14. public $var8 = array(true, false);
  15. }
  16. ?>

2、new

要建立一個對象的執行個體,必須建立一個新對象並將其賦給一個變數。當建立新對象時該對象總是被賦值,除非該對象定義了建構函式並且在出錯時拋出了一個異常。

Example#4 建立一個執行個體

 
  1. $instance = new SimpleClass();
  2. ?>

複製代碼當把一個對象已經建立的執行個體賦給一個新變數時,新變數會訪問同一個執行個體,就和用該對象賦值一樣。此行為和給函數傳遞入執行個體時一樣。可以用複製給一個已建立的對象建立一個新執行個體。

Example#5 對象賦值

 
  1. $assigned = $instance;
  2. $reference =& $instance;
  3. $instance->var = '$assigned will have this value';
  4. $instance = null; // $instance and $reference become null
  5. var_dump($instance);
  6. var_dump($reference);
  7. var_dump($assigned);
  8. ?>

複製代碼上例將輸出:

 
  1. NULL
  2. NULL
  3. object(SimpleClass)#1 (1) {
  4. ["var"]=>
  5. string(30) "$assigned will have this value"
  6. }

3、extends

一個類可以在聲明中用 extends 關鍵字繼承另一個類的方法和成員。不能擴充多個類,只能繼承一個基類。

被繼承的方法和成員可以通過用同樣的名字重新聲明被覆蓋,除非父類定義方法時使用了 final 關鍵字。可以通過 parent:: 來訪問被覆蓋的方法或成員。

Example#6 簡單的類繼承

 
  1. class ExtendClass extends SimpleClass
  2. {
  3. // Redefine the parent method
  4. function displayVar()
  5. {
  6. echo "Extending classn";
  7. parent::displayVar();
  8. }
  9. }
  10. $extended = new ExtendClass();
  11. $extended->displayVar();
  12. ?>

上例將輸出:

 
  1. Extending class
  2. a default value

1

http://www.bkjia.com/PHPjc/445764.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445764.htmlTechArticlePHP 5 引入了新的物件模型(Object Model)。完全重寫了 PHP 處理對象的方式,允許更佳效能和更多特性。 一、基本概念 1、class 每個類的定義...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.