Php-mysql Course

Inheritance is one of the most useful tools of Object Oriented Programming - OOP.
Inheritance enables you to define a base class (also called parent class) and create one or more classes derived from it.
A class that inherits from another is said to be a subclass of it, or child class.
A child class inherits and can use all public and protected properties and methods from the parent, without having to copy any code. That way, you need to write only the additional properties and methods of the child class.

Creating a child class

To create a child class, you must use the extends keyword in the subclass declaration.
  - Syntax:
class ChildClass extends ParentClass {
  // subclass body
}
The extends keyword tells PHP that the ChildClass should inherit all the properties and methods of the ParentClass (those with public or protected attribute).

  - Example
In this example we create a base class, called Sites, with two properties ("site" with private attribute, and "category" with public attribute), constructor, and a public method, getPag().
Then we create a subclass derived from this class.
<?php
// Sites class
class Sites {
  // private and public properties
  private $site;
  public $category = 'php-mysql';

  // Constructor
  public function __construct($site) {
    // if the $site argument is string type, with more than 3 characters, assigns its value to $site property
    // else, display a message and ends the script
    if(is_string($site) && strlen($site)>3) $this->site = $site;
    else exit('Invalid value for $site');

  }

  // getPag() - public
  public function getPag($pag) {
    // return a url address, with the two properies and its argument
    $url = $this->site. '/'. $this->category. '/'. $pag;
    return $url;
  }
}
?>
- The __construct() receives one argument and use it to set the value of "$site" property. The getPag() method defines a URL address, with the two properies and its argument, and return it.
We save this clas in class.Sites.php file.

Now, the code for child class, called LinkSites.
The base class can be used to define an URL address. We create a subclass with only one method, "getLink()" which returns a link (HTML <a> tag) with the URL defined in the base class.
To can access the definitions of the parent class, becouse it is in an external file, we must include it with include().
<?php
include('class.Sites.php');        // Include parent class (Sites)

// child class, LinkSites, derived from Sites
class LinkSites extends Sites {
  public function getLink($pag) {
    // output an HTML link, using getPag() and "category" declared in parent class
    echo '<a href="http://'. $this->getPag($pag). '" title="'. $this->category. '">Link</a><br />';
  }
}
?>
- The getLink() method uses the "category" property and the "getPag()" method declared in parent class, as if it were defined in the subclass.
Although this child class does not have a constructor method, it inherits the parent class's constructor, so when you create an object instance of the LinkSites class you must add an argument (string type).
• If the subclass do not define constructors, the parent class’s constructor is automatically invoked when the child class is instantiated.
Save this child class in class.LinkSites.php.

We use the following script to test the child class (LinkSites).
<?php
include('class.LinkSites.php');      // Include LinkSites subclass

// object instance of the LinkSites
$objLink = new LinkSites('https://coursesweb.net');

// calls getLink() (defined in child class)
$objLink->getLink('php-oop-inheritance-class-extends');

// change the value of the "category" (which is in the parent class)
$objLink->category = 'ajax';

// calls getPag() (which is in the parent class)
echo $objLink->getPag('xmlhttprequest-object');
?>
As you can notice, the object instance of the child class can use the public properties and methods of the parent class as if it were created in the subclass.
The example above will output:
<a href="https://coursesweb.net/php-mysql/php-oop-inheritance-class-extends" title="php-mysql">Link</a>
https://coursesweb.net/ajax/xmlhttprequest-object

Overriding methods

In the example above, the child class inherited the __construct() method of the parent class, that will cause the execution of the code of that constructor when an object instance of child class is created.
If you don't want to use a property or method like it is defined in the parent class, you can override it by adding one with the same name in the child class, thus, when the object instance is created, will use the method or property with the same name defined in the child class.
• Overriding a method does not affect with nothing the parent method, the changes made ​​are only available in the child class where it's rewritten.

If you want to override a parent method and also to use its functionality in the subclass code, you can access the parent method using the parent keyword followed by ::
  - Syntax:
parent::methodName()
  - Example
In this example we create another subclass, called PagSites, derived from Sites class. In this child class we override the __construct() and getPag() methods.
<?php
include('class.Sites.php');        // Include parent class (Site)

// child class, PagSites, derived from Sites
class PagSites extends Sites {
  public $domain = 'marplo.net';       // Proprietate

  // override the parent constructor
  function __construct() {
    // with no instructions, does nothing
  }

  // override getPag()
  public function getPag($pag) {
    // gets the result returned by the getPag() from the parent class
    $url = parent::getPag($pag);

    // add to $url the value of "$domain"
    $url = $this->domain. $url;

    return 'Page: '. $url;
  }
}
?>
The __construct() declared in the subclass removes the effect of the constructor from the parent class. Becouse here it is defined without parameter, when an object instance of this subclass is created it's no need to add an argument.
The parent::getPag($pag) instruction gets / implements the result returned by the getPag() method of the parent class.

In the next script we use the PagSites subclass.
<?php
include('class.PagSites.php');      // Include PagSites subclass

// object instance of the PagSites
$objPag = new PagSites();

// change the value of the "category" (which is in the parent class)
$objPag->category = 'html';

// prints the result returned by getPag() (Overrided)
echo $objPag->getPag('introducere.html');
?>
The $objPag->getPag('introducere.html') uses the getPag() method defined in the PagSites subclass.
This example will output:
Page: marplo.net/html/introducere.html

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 - Inheritance, class extends

Last accessed pages

  1. The Essene Gospel of Peace (2504)
  2. CSS3 Flexbox Container (1086)
  3. Movie Clip Symbols (2327)
  4. PHP OOP - Constructor Method (7515)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142533)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (536)
  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)