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-27. 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-28. 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) ?>
|
|
Copyright © 1997 - 2007 by the PHP Documentation Group. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later. A copy of the Open Publication License is distributed with this manual, the latest version is presently available at http://www.opencontent.org/openpub/.
Please see full copyright text at http://www.php.net/manual/en/copyright.php
Original version of the above documentation is available at http://www.php.net/manual/en/