Final classes

Inheritance allows for enormous flexibility within a class hierarchy. You can create subclasses to extend the functionality of a base class, but PHP OOP gives the posibility to create classes that cannot be extended. Such a class is called final class.
A final class is declared by adding the final keyword before the class word.
  - For example:
// base class (declared final)
final class Base {
  // ... PHP code

// define a child class derivated from Base
class BaseChild extends Base {
  // ... PHP instructions
Running this code will generate the following error:
Fatal error: Class BaseChild may not inherit from final class (Base) in ...

Final methods

A normal method (public or protected) can be overridden in the child class. If you want a method to remain fixed and unchanging, prefix the definition with the final keyword.
A final method cannot be overridden.
  - For example:
// base class
class Base {
  // final method
  final public function testMethod() {
    echo 'This is a final method';

// define a child class derivated from Base
class BaseChild extends Base {
  // override the testMethod()
  public function testMethod() {
    echo 'Another text';
This example will generate the following error:
Fatal error: Cannot override final method Base::testMethod() in ...

By declaring a class or method final, you limit the flexibility given by inheritance, so, use final with care.

Daily Test with Code Example

What attribute makes the input text box to not be modified by the user?
checked="checked" readonly="readonly" disabled="disabled"
<input type="text" value="fixed-value" readonly="readonly" name="a_name" />
What CSS property allows you to create rounded corners in your webpage design?
background-size border-size border-radius
.class {
  border:2px solid blue;
What instruction displays a confirmation dialog box to the viewer, who must then click OK or Cancel to proceed?
indexOf() confirm() prompt()
var ques = window.confirm("The result of 0+0 is 0?");
if (ques) alert("Corect");
else alert("Incorrect");
Indicate the PHP function that returns the lowest number of the parameter values.
floor() ceil() min()
$min_nr = min(12, 8, 25, 13);
echo $min_nr;        // 8
PHP OOP - Final Classes and Methods

Last accessed pages

  1. Simple PHP Upload Script (321)
  2. Display / Simulate a Loading Progress Bar (56)
  3. CSS Float and Clear (102)
  4. jQuery get XML data (149)
  5. Ajax-PHP Rating Stars Script (562)

Top accessed pages

  1. PHP-MySQL free course, online tutorials PHP MySQL code (1768)
  2. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (1730)
  3. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (1683)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (1420)
  5. PHP Chat Script (1353)