PHP/Class/final
Содержание
Declaring Final Classes and Methods
<source lang="html4strict">
<?php
final class NoExtending { public function myFunction() { } } class restrictedExtending { final public function anotherFunc() { } } class myChild extends restrictedExtending { public function thirdFunction() { } }
?>
</source>
final class
<source lang="html4strict">
<?php final class Checkout {
// ...
} class IllegalCheckout extends Checkout {
// ...
} $checkout = new Checkout(); ?>
</source>
final keyword can be used to declare a class uninheritable
<source lang="html4strict">
<?
final class Dog { private $Name; public function getName( ) { return $this->Name; } } class Poodle extends Dog { public function bark( ) { print ""Woof", says " . $this->getName( ); } }
?>
</source>
Final Methods
<source lang="html4strict">
class Item {
private $id = 555; final function getID() { return $this->id; }
}
class PriceItem extends Item {
function getID() { return 0; }
} It generates the following error: Fatal error: Cannot override final method item::getid()
</source>
The final keyword is used to declare that a method or class cannot be overridden by a subclass
<source lang="html4strict">
<?
class Dog { private $Name; private $DogTag; final public function bark( ) { print "Woof!\n"; } }
?>
</source>