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 create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
}
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
PHP OOP - Final Classes and Methods

Last accessed pages

  1. Ajax-PHP File Manager (9853)
  2. Display data from PHP Array, or MySQL in HTML table (26305)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (134087)
  4. Display / Simulate a Loading Progress Bar (2552)
  5. Zodiac Signs JavaScript code (10631)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (384)
  2. SHA1 Encrypt data in JavaScript (292)
  3. PHP Unzipper - Extract Zip, Rar Archives (275)
  4. SHA256 Encrypt hash in JavaScript (264)
  5. Read Excel file data in PHP - PhpExcelReader (245)