PHP class 基本範例 (USBWebserver下的測試目錄php_class_learn)
PHP class 基本範例 (USBWebserver下的測試目錄php_class_learn)
資料來源: http://pydoing.blogspot.tw/2013/03/PHP-Class-and-Object.html
01.php
<?php class Demo { private $a = 11; private $b = 22;
function do_something() { return $this->a + $this->b; } }
$demo = new Demo(); echo $demo->do_something();
/* 資料來源:http://pydoing.blogspot.tw/2013/03/PHP-Class-and-Object.html 包含技術: 01.class 基本定義 02.變數和函數的存取權限定義 03.成員變數初值定義 04.class實體化的基本教學 05.物件實際呼叫成員應用 */ ?> |
02.php
<?php class Demo { const c = “constant value\n”;
function do_something() { return self::c; } }
echo Demo::c; $d = “Demo”; echo $d::c; $o = new Demo(); echo $o::c; echo $o->do_something();
/* 資料來源:http://pydoing.blogspot.tw/2013/03/PHP-const.html 包含技術: 01.常數(const)定義與使用 */ ?> |
03.php
<?php class Demo { function __construct() { echo “constructor call…\n”; }
function __destruct() { echo “destructor call…\n”; }
function do_something() { echo “do something\n”; } }
$o = new Demo(); $o->do_something();
/* 資料來源:http://pydoing.blogspot.tw/2013/03/PHP-Constructor-and-Destructor.html 包含技術: 01.建構子/解構子 */ ?> |
04.php
<?php class Demo { public function __call($name, $arguments) { echo “something wrong”; } }
$d = new Demo(); $d->do_something();
/* 資料來源:http://pydoing.blogspot.tw/2013/03/PHP-Magic-Method.html 包含技術: 01.內建方法 方法 說明 __call($name, $arguments) 當呼叫不存在的方法時執行 __callStatic($name, $arguments) 當呼叫不存在的 static 方法時執行 __get($name) 當讀取不存在的屬性時執行 __set($name, $value) 當設定不存在的屬性時執行 __isset($name) 當使用不存在的屬性呼叫 isset() 或 empty() 時執行 __unset($name) 當使用不存在的屬性呼叫 unset() 時執行 __sleep() 在物件序列化之前執行 __wakeup() 在物件序列化過程中重建物件 __toString() 物件的字串形式 __invoke($x) 當把物件當函數呼叫時執行 __set_state($an_array) 當呼叫 var_export() 方法時執行 __clone() 使用 clone 複製物件完成後執行 */ ?> |
05.php
<?php class Demo { private $a = “superclass_a”; protected $b = “superclass_b”; public $c = “superclass_c”;
function do_something() { echo “a: $this->a, b: $this->b, c: $this->c\n”; } }
class Demo2 extends Demo { function do_something() { echo “a: $this->a, b: $this->b, c: $this->c\n”; } }
$o = new Demo2(); $o->do_something();
/* 資料來源:http://pydoing.blogspot.tw/2013/03/PHP-Inheritance.html 包含技術: 01.類別繼承 執行結果 Demo 中已經有定義 do_something() 方法 (method) , Demo2 再次定義相同名稱 do_something() 會改寫父類別的 do_something() ,因此子類別的物件呼叫 do_something() 會是子類別定義的版本。 屬性 (property) $a 沒有被印出,這是因為 $a 在父類別 Demo 為 private 成員,因此子類別 Demo2 並沒有繼承 $a ,就 Demo2 而言, $a 為一個沒有被定義的屬性。 心得 繼承特性同等C++的public繼承 */ ?> |
06.php
<?php class Demo { public static $zero = 0;
public static function do_something() { return self::$zero; } }
echo Demo::$zero . “\n”; echo Demo::do_something() . “\n”; $d = new Demo(); echo $d->zero . “\n”; echo $d->do_something();
/* 資料來源:http://pydoing.blogspot.tw/2013/03/PHP-static.html 包含技術: 01.靜態(static)成員 */ ?> |
07.php
<?php interface Demo { const n = 100; public function do_something(); }
class Demo2 implements Demo { function do_something() { return self::n; } }
$d = new Demo2(); echo Demo::n . “\n”; echo $d->do_something();
/* 資料來源:http://pydoing.blogspot.tw/2013/03/PHP-interface.html 包含技術: 01.介面(interface) */ ?> |
08.php
<?php interface Demo { public function do_something1(); }
interface Demo2 { public function do_something2(); }
interface Demo3 extends Demo, Demo2 { public function do_something3(); }
class Demo4 implements Demo3 { function do_something1() { return “1”; }
function do_something2() { return “2”; }
function do_something3() { return “3”; } }
$d2 = new Demo4(); echo $d2->do_something1() . “\n”; echo $d2->do_something2() . “\n”; echo $d2->do_something3();
/* 資料來源:http://pydoing.blogspot.tw/2013/03/PHP-interface.html 包含技術: 01.介面(interface)和介面繼承 */ ?> |