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 defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
PHP OOP - Inheritance, class extends

Last accessed pages

  1. Redirects (4758)
  2. SSEP - Site Search Engine PHP-Ajax (11366)
  3. Remove / Get duplicate array values - Reset array keys in PHP (13020)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137122)
  5. Integer and Float value in Select with PDO from string to numeric (8548)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (320)
  2. PHP Unzipper - Extract Zip, Rar Archives (110)
  3. Read Excel file data in PHP - PhpExcelReader (101)
  4. SHA1 Encrypt data in JavaScript (81)
  5. Get and Modify content of an Iframe (74)
Chat
Chat or leave a message for the other users
Full screenInchide