iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 23
0
Modern Web

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

Day23 - PHP-物件導向(OOP)介紹-Part3

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

六、類別繼承 Class Inheritance


使用extends可以讓類別(class)繼承其他類別的方法(methods)和屬性(property)。

以上次例子繼續來建立一個新的類別並且繼承(extends)MyClass,
新的類別可以使用MyClass原有的方法(methods)和屬性(property)

<?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;
    }

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

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


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

echo $obj->newMethod();                   //呼叫物件到新類別的`newMethod()`。

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

輸出到畫面會顯示

The class "MyClass" was initiated!
From a new method in "MyNewClass".
I'm a class property!
The class "MyClass" was destroyed.

使用覆寫(Override) 繼承的方法和屬性

還可以在新類別裡面重新定義從父類別繼承來的屬性和方法的行為

<?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;
    }

    public 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->newMethod();                   //呼叫物件到新類別的`newMethod()`。

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

輸出到畫面會顯示

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

此篇大家可以多多練習用不同的方式去繼承修改繼承覆寫
最近篇幅都不長是因為想放慢腳步好好了解自己是不是真的有把這些專業知識都放到腦帶裡,
本篇介紹到此,下次見~


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

尚未有邦友留言

立即登入留言