News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Andrew Podner:
Using Final to Prevent Overrides and Extension
November 26, 2012 @ 10:36:05

In the latest post to his site Andrew Podner takes a quick look at something you don't see too much in PHP applications but is a familiar concept to some coming in to the language from others - using "final" to prevent overrides of the code in your classes.

In a previous post about inheritance, I showed you how to extend a class. One aspect of extending classes that wasn't fully covered was the idea of overriding a method in a class. You can override a method (function) from the base class by simply redefining the method in the child class. [...] There are times though, when you do not want a method to ever be overridden. There may even be cases where you do not want a class to be extended.

His example shows how to use this "final" keyword on a database class, protecting a method (getRecord) that could potentially break the application if changed. This would then give the developer trying to extend the class an error noting that that method cannot be overridden. One thing to note, if you're going to use "final" in your code, be sure you know what you're doing. More often than not, you probably just want something like "private" or "protected" (see this post for a bit more explanation).

0 comments voice your opinion now!
final class method tutorial visibility extend override


blog comments powered by Disqus

Similar Posts

WebDigity.com: PHP geotargeting techniques

Brandon Savage's Blog: Making Life Better With The SPL Autoloader

PHP-GTK Community Site: GtkToolbar, GtkMenubar, and PEAR XML-RPC for geo-nick

Matthew Weier O'Phinney's Blog: Autocompletion with Zend Framework and Dojo

Gonzalo Ayuso's Blog: Building an ORM with PHP


Community Events





Don't see your event here?
Let us know!


list application install code symfony introduction podcast community developer bugfix language series configure unittest release laravel interview library api wordpress

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework