PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Object cloning> <Magic Methods
Last updated: Mon, 26 Nov 2007

view this page in

Final Keyword

PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

Example#1 Final methods example

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }
   
   final public function 
moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class 
ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() called\n";
   }
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>

Example#2 Final class example

<?php
final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() called\n";
   }

   
// Here it doesn't matter if you specify the function as final or not
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() called\n";
   }
}

class 
ChildClass extends BaseClass {
}
// Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)
?>


Object cloning> <Magic Methods
Last updated: Mon, 26 Nov 2007
 
add a note add a note User Contributed Notes
Final Keyword
someone3x7 at hotmail dot com
06-Mar-2008 02:32
This keyword goes against the principles of object oriented methodology. No class is so sacred it can't be allowed to evolve. No programmer is so omniscient to know what will be needed 5 years (let alone 5 minutes) from release. 'Final' should be disabled and depreciated.
slorenzo at clug dot org dot ve
31-Oct-2007 03:13
<?php
class parentClass {
    public function
someMethod() { }
}
class
childClass extends parentClass {
    public final function
someMethod() { } //override parent function
}

$class = new childClass;
$class->someMethod(); //call the override function in chield class
?>
penartur at yandex dot ru
22-Mar-2007 05:39
Note that you cannot ovverride final methods even if they are defined as private in parent class.
Thus, the following example:
<?php
class parentClass {
    final private function
someMethod() { }
}
class
childClass extends parentClass {
    private function
someMethod() { }
}
?>
dies with error "Fatal error: Cannot override final method parentClass::someMethod() in ***.php on line 7"

Such behaviour looks slight unexpected because in child class we cannot know, which private methods exists in a parent class and vice versa.

So, remember that if you defined a private static method, you cannot place method with the same name in child class.
liumr at ustc dot edu
11-Mar-2007 02:29
this key word can describe a class or a class member function

Object cloning> <Magic Methods
Last updated: Mon, 26 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites