PHP8 對象繼承

2023-08-16 17:54 更新

繼承已為大家所熟知的一個(gè)程序設(shè)計(jì)特性,PHP 的對象模型也使用了繼承。繼承將會影響到類與類,對象與對象之間的關(guān)系。

比如,當(dāng)擴(kuò)展一個(gè)類,子類就會繼承父類所有 public 和 protected 的方法,屬性和常量。除非子類覆蓋了父類的方法,被繼承的方法都會保留其原有功能。

繼承有助于功能的設(shè)計(jì)和抽象,在實(shí)現(xiàn)類似的對象、增加新功能時(shí),無須重復(fù)編寫這些公用的功能。

子類無法訪問父類的私有方法。因此,子類無需考慮正常的繼承規(guī)則而重新實(shí)現(xiàn)私有方法。 然而,在 PHP 8.0.0 之前, final 和 static 的限制會應(yīng)用于 private 方法。 從 PHP 8.0.0 開始,僅 private final 的構(gòu)造器是唯一受限的 private 方法; 想要“禁用”構(gòu)造器,我們通常用靜態(tài)工廠方法作為代替。

方法,屬性和常量的 可見性 可以放寬,例如 protected 方法可以標(biāo)記為 public, 但不能增加限制,例如標(biāo)記 public 屬性為 private。有個(gè)例外是構(gòu)造方法,可以限制其可見性,例如 public 構(gòu)造方法可以在子類中標(biāo)記為 private。

注意:除非使用了自動加載,否則一個(gè)類必須在使用之前被定義。如果一個(gè)類擴(kuò)展了另一個(gè),則父類必須在子類之前被聲明。此規(guī)則適用于類繼承其它類與接口。
注意:It is not allowed to override a read-write property with a readonly property or vice versa. readonly public readonly int $prop;}?>

示例 #1 繼承示例

<?php

class Foo
{
public function printItem($string)
{
echo 'Foo: ' . $string . PHP_EOL;
}

public function printPHP()
{
echo 'PHP is great.' . PHP_EOL;
}
}

class Bar extends Foo
{
public function printItem($string)
{
echo 'Bar: ' . $string . PHP_EOL;
}
}

$foo = new Foo();
$bar = new Bar();
$foo->printItem('baz'); // 輸出: 'Foo: baz'
$foo->printPHP(); // 輸出: 'PHP is great'
$bar->printItem('baz'); // 輸出: 'Bar: baz'
$bar->printPHP(); // 輸出: 'PHP is great'

?>

Return Type Compatibility with Internal Classes

Prior to PHP 8.1, most internal classes or methods didn't declare their return types, and any return type was allowed when extending them.

As of PHP 8.1.0, most internal methods started to "tentatively" declare their return type, in that case the return type of methods should be compatible with the parent being extended; otherwise, a deprecation notice is emitted. Note that lack of an explicit return declaration is also considered a signature mismatch, and thus results in the deprecation notice.

If the return type cannot be declared for an overriding method due to PHP cross-version compatibility concerns, a ReturnTypeWillChange attribute can be added to silence the deprecation notice.

示例 #2 The overriding method does not declare any return type

<?php
class MyDateTime extends DateTime
{
public function modify(string $modifier) { return false; }
}

// "Deprecated: Return type of MyDateTime::modify(string $modifier) should either be compatible with DateTime::modify(string $modifier): DateTime|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice" as of PHP 8.1.0
?>

示例 #3 The overriding method declares a wrong return type

<?php
class MyDateTime extends DateTime
{
public function modify(string $modifier): ?DateTime { return null; }
}

// "Deprecated: Return type of MyDateTime::modify(string $modifier): ?DateTime should either be compatible with DateTime::modify(string $modifier): DateTime|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice" as of PHP 8.1.0
?>

示例 #4 The overriding method declares a wrong return type without a deprecation notice

<?php
class MyDateTime extends DateTime
{
/**
* @return DateTime|false
*/
#[\ReturnTypeWillChange]
public function modify(string $modifier) { return false; }
}

// No notice is triggered
?>


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號