認識 PHP 物件導向(一)

前言

本文為〈PHP OO 基礎教學〉一文的學習筆記。

類別與物件

類別(Class):可以比喻為一間房子的藍圖,定義房子的結構與形狀。

物件(Object):可以比喻為一棟真的房子,是類別的實例化。

建立類別

1
2
3
4
5
6
7
8
class MyClass
{
//
}

$obj = new MyClass;

var_dump($obj);

結果:

1
2
object(MyClass)#1 (0) {
}

定義類別屬性

屬性(Property):也稱作類別的變數。

1
2
3
4
5
6
7
8
class MyClass
{
public $prop = "I'm a class property!";
}

$obj = new MyClass;

var_dump($obj);

結果:

1
2
3
4
object(MyClass)#1 (1) {
["prop"]=>
string(21) "I'm a class property!"
}

使用箭頭「->」來存取物件的屬性和方法。

1
2
3
4
5
6
7
8
class MyClass
{
public $prop = "I'm a class property!";
}

$obj = new MyClass;

echo $obj->prop;

結果:

1
I'm a class property!

定義類別方法

方法(Method):類別裡面的函式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class MyClass
{
public $prop = "I'm a class property!";

public function setProperty($new)
{
$this->prop = $new;
}

public function getProperty()
{
return $this->prop . "<br />";
}
}

$obj = new MyClass;

echo $obj->getProperty();

$obj->setProperty("I'm a new class property!");

echo $obj->getProperty();
  • $this 就如同直接使用物件名稱來指定物件。

結果:

1
2
I'm a class property!
I'm a new class property!

實例化第二個類別:

1
2
3
4
5
6
7
$obj2 = new MyClass;

echo $obj2->getProperty();

$obj2->setProperty("I belong to the second instance!");

echo $obj2->getProperty();

結果:

1
2
3
4
I'm a class property!
I'm a new class property!
I'm a class property!
I belong to the second instance!

魔術函數

  1. __construct() 建構子,類別一旦宣告後就直接執行,用來執行必要動作如:資料庫連線等。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class MyClass
{
public $prop = "I'm a class property!";

public function __construct()
{
echo 'The class "', __CLASS__, '" was initiated!<br />';
}

public function setProperty($new)
{
$this->prop = $new;
}

public function getProperty()
{
return $this->prop . "<br />";
}
}

$obj = new MyClass;

echo $obj->getProperty();

$obj->setProperty("I'm a new class property!");

echo $obj->getProperty();
  • __CLASS__ 魔術常數(Magic constant),會回傳被呼叫的類別的名稱。

結果:

1
2
3
The class "MyClass" was initiated!
I'm a class property!
I'm a new class property!
  1. __destruct() 解構子,用來執行收尾動作如:關閉資料庫連線等。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class MyClass
{
public $prop = "I'm a class property!";

public function __construct()
{
echo 'The class "', __CLASS__, '" was initiated!<br />';
}

public function __destruct()
{
echo 'The class "', __CLASS__, '" was destroyed!<br />';
}

public function setProperty($new)
{
$this->prop = $new;
}

public function getProperty()
{
return $this->prop . "<br />";
}
}

$obj = new MyClass;

echo $obj->getProperty();

$obj->setProperty("I'm a new class property!");

echo $obj->getProperty();

結果:

1
2
3
4
The class "MyClass" was initiated!
I'm a class property!
I'm a new class property!
The class "MyClass" was destroyed!
  1. __toString(),當物件被當作字串處理時觸發,可以將物件轉換為字串。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class MyClass
{
public $prop = "I'm a class property!";

public function __construct()
{
echo 'The class "', __CLASS__, '" was initiated!<br />';
}

public function __destruct()
{
echo 'The class "', __CLASS__, '" was destroyed.<br />';
}

public function __toString()
{
echo "Using the toString method.<br />";
return $this->getProperty();
}

public function setProperty($new)
{
$this->prop = $new;
}

public function getProperty()
{
return $this->prop . "<br />";
}
}

$obj = new MyClass;

echo $obj;

結果:

1
2
3
4
The class "MyClass" was initiated!
Using the toString method.
I'm a class property!
The class "MyClass" was destroyed.

摧毀物件

1
unset($obj);