Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / PHP

Słowo kluczowe extends

Klasa rozszerza jedną bezpośrednią klasę bazową i dziedziczy jej dostępną implementację.

Wersja: PHP 8.4; repository runtime, production runtime not attested

Składnia

class PriorityOrder extends Order {}

Klasa rozszerza jedną bezpośrednią klasę bazową i dziedziczy jej dostępną implementację. Nadpisana metoda może wywołać poprzednią implementację przez parent::. Sygnatura musi pozostać zgodna. Interfejs może rozszerzać kilka interfejsów.

class Order {protected function base():int{return 10;} public function total():int{return $this->base();}}
class PriorityOrder extends Order {public function total():int{return parent::total()+3;}}
echo (new PriorityOrder())->total();
13

Przykłady

<?php
class Order {protected function base():int{return 10;} public function total():int{return $this->base();}}
class PriorityOrder extends Order {public function total():int{return parent::total()+3;}}
echo (new PriorityOrder())->total();

Wynik

13

Źródła