Php-mysql Course

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:
<?php
// 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:
<?php
// 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

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
PHP OOP - Final Classes and Methods

Last accessed pages

  1. CSS Content (425)
  2. Animated Tooltip with HTML and CSS (284)
  3. MySQLDumper - Backup MySQL Database (1782)
  4. querySelector and querySelectorAll (30136)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142540)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (543)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)