iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 25
0
Modern Web

VUE & PHP (Apache2) & Docker 實戰開發系列 第 25

Day25 - PHP-物件導向(OOP)介紹-Part4

本篇接續上篇PHP-物件導向(OOP)介紹-Part3

七、屬性和方法的可視性(The Visibility of Properties and Methods)


加入可視性(Visibility),可視性有三個public、protected、private
可以決定能不能從物件外面控制物件的方法和屬性。

1.Public Properties and Methods

當一個變數或方法的可視性被宣告為public。這表示這些方法和屬性可以在類別的裡面與外面被存取
前幾篇了範例都是使用public,新的類別以及所有的子類別都可使用被宣告為public的屬性或方法。

2.Protected Properties and Methods

當一個變數或方法的可視性被宣告為protected,該變數或方法只能在類別以及子類別的內部存取

下方範例替換MyClass的getProperty()的可視性宣告為protected,並且試著從外部呼叫

<?php
class MyClass
{
    public $prop1 = "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: ";
        return $this->getProperty();
    }

    public function setProperty($newval)
    {
        $this->prop1 = $newval;
    }

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

class MyNewClass extends MyClass
{
    public function __construct()
    {
        echo 'A new constructor in "'. __CLASS__ . '".<br />';
    }
        
    public function newMethod()           //在新類別裡宣告一個屬性與方法。
    {
        echo 'From a new method in "' . __CLASS__ . '".<br />';
    }
}

$obj = new MyNewClass;                    //建立`MyNewClass`的新物件。

echo $obj->getProperty();                 //呼叫繼承父類別的`getProperty()`。

輸出到畫面會顯示Call to protected method的錯誤訊息

A new constructor in "MyNewClass".
Fatal error: Uncaught Error: Call to protected method MyClass::getProperty() from context '' in /Users/caizhiwei/test.php:48 Stack trace: #0 {main} thrown in /Users/caizhiwei/test.php on line 48

這裡我們在子類別MyNewClass中新增一個public來調用getProperty()

<?php
class MyClass
{
    public $prop1 = "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: ";
        return $this->getProperty();
    }

    public function setProperty($newval)
    {
        $this->prop1 = $newval;
    }

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

class MyNewClass extends MyClass
{
    public function __construct()
    {
        echo 'A new constructor in "'. __CLASS__ . '".<br />';
    }
        
    public function newMethod()           //在新類別裡宣告一個屬性與方法。
    {
        echo 'From a new method in "' . __CLASS__ . '".<br />';
    }
    
    public function getProperty()         //使用`public`從父類別繼承`getProperty()`。
    {
        return $this->prop1 . "<br />";
    }
}

$obj = new MyNewClass;                    //建立`MyNewClass`的新物件。

echo $obj->getProperty();                 //呼叫繼承父類別的`getProperty()`。

輸出到畫面會顯示

A new constructor in "MyNewClass".
I'm a class property!
The class "MyClass" was destroyed.

由上述可見,子類別可以調用父類別的protected可視性。

3.Private Properties and Methods

當一個變數或方法的可視性被宣告為private,該變數或方法只能在定義它們的類別內
如果有一個新的類別繼承了宣告有private屬性或方法的類別,
新的類別以及所有的子類別將沒有辦法使用這些被宣告為private的屬性或方法。

結語、物件導向與直譯程式的差別


這兩種寫程式的方法沒有對和錯,只是如果使用物件導向來撰寫程式,在開發大型專案時會比較容易管理,
因為物件可以將資料儲存在內部屬性內,因此物件內部的方法不必一直把屬性裝入參數內就可以運行了,
多個屬於同一個類別的物件可以同時存在,這在處理大量數據組合的時候可以把程式變得更加簡化。
物件導向運用得正確會讓你的程式變易讀、易維護、易攜帶、可重複使用,這會減少你很多花額外的時間。

這些都是需要經過不斷的思考及不斷地練習才用運用自如,基本功還是少不了的可以多看點其他大神的文章,
剛開始或許會有些疑惑,不妨嘗試去修改或新增找出前後的差異才能一點一滴的累積實力,誰不是這樣過來的呢。
本篇介紹到此,下次見~


上一篇
Day24 - Vue & Azure Docker Registry
下一篇
Day26 - PHP & Docker
系列文
VUE & PHP (Apache2) & Docker 實戰開發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言