PHP/Class/final

Материал из Web эксперт
Версия от 10:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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>