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

Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="" title=""></a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr = array(1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
PHP OOP - Final Classes and Methods

Last accessed pages

  1. Bubble Tanks 3 (4307)
  2. CSS Border (1692)
  3. Script Users Register, Login, Online (4947)
  4. innerHTML in PHP (1164)
  5. innerHTML and outerHTML to Get and Replace HTML content (2393)

Top accessed pages

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