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 HTML element can be used to embed a SWF flash content?
<object> <div> <script>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250">
 <param name="src" value="file.swf" />
 Your browser not support SWF.
</object>
Which CSS pseudo-class adds a style to an input form field that has keyboard input focus?
:active :focus :hover
input:focus {
  background-color: #88fe88;
}
Click on the instruction which converts a JSON string into a JavaScript object.
JSON.stringify(javascript_object) object.toString() JSON.parse(json_string)
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}';
var obj = JSON.parse(jsnstr);
alert(obj.url);
Indicate the PHP function which can be used to create or write a file on server.
fopen() file_put_contents() file_get_contents()
if (file_put_contents("file.txt", "content")) echo "The file was created";
else echo "The file can not be created";
PHP OOP - Final Classes and Methods

Last accessed pages

  1. JavaScript Course - Free lessons (31647)
  2. Volume and Surface Area Calculator for 3D objects (11276)
  3. Using v-model in form input fields (1051)
  4. jQuery UI draggable - Drag elements (11445)
  5. Display data from PHP Array, or MySQL in HTML table (26980)

Popular pages this month

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