downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  

<Magic MethodsObject cloning>
Last updated: Sat, 29 Oct 2005

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 19-28. 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 19-29. 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)
?>


add a note add a note User Contributed Notes
Final Keyword
There are no user contributed notes for this page.




<Magic MethodsObject cloning>
Last updated: Sat, 29 Oct 2005
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://phpbuilder.com/
Last updated: Tue Nov 1 20:20:59 2005 EST
Columns / Articles | Tips / Quickies | News | News Linking and RSS Feeds | Shared Code Library
Mail Archives | Support / Discussion Forums | Get Started! Links | Contribute! | Docs