Hudzilla.org - the homepage of Paul Hudson
Contents > Objects > Access control modifiers Wish List | Report Bug | About Me ]

6.7.4     Final

This is NOT the latest copy of this book; click here for the latest version.

The final keyword is used to declare that a function or class cannot be overriden by a sub-class. This is another way of stopping other programmers using your code outside the bounds you had planned for it.

Take a look at the following code:

class dog {
    private
$Name;
    private
$DogTag;
    final public function
bark() {
        print
"Woof!\n";
    }
    

The dog bark() function is now declared as being final, which means it cannot be overridden in a child class. If we have bark() redefined in the poodle class, PHP outputs a fatal error message: Cannot override final method dog::bark(). Using the final keyword is entirely optional, but it makes your life easier by acting as a safeguard against people overriding a function you believe should be permanent.

For stronger protection, the final keyword can also be used to declare a class as uninheritable - that is, that programmers cannot extend another class from it. Take a look at this script:

<?php
    
final class dog {
        public
$Name;
        private function
getName() {
            return
$this->Name;
        }
    }

    class
poodle extends dog {
        public function
bark() {
            print
"'Woof', says " . $this->getName();
        }
    }
?>

Attempting to run that script will result in a fatal error, with the message "Class poodle may not inherit from final class (dog)".





<< 6.7.3 Protected   6.7.5 Abstract >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
SID TRIVEDI - 05 Dec 2008

<?php
class dog {
public $Name;
public function getName() {
return $this->Name;
}
public function bark() { # public declaration for first time function brak() definition.
print "'Woof', says " . $this->getName();
}
}

class poodle extends dog {
final public function bark() { # final declaration for last time function brak() definition.
print "'Yeep', says " . $this->getName();
}
}

$poppy = new dog;
$poppy->Name = "Poppy";
$poppy->bark();
echo "\n<br>";
$penny = new poodle;
$penny->Name = "Penny";
$penny->bark();

?>



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow