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 to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
PHP OOP - Inheritance, class extends

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141749)
  2. Node.js Move and Copy file (28420)
  3. MouseEvent - Events for Mouse (2909)
  4. PHPMailer (2311)
  5. Uploading images to server with Ajax (6095)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (473)
  2. CSS cursor property - Custom Cursors (79)
  3. The Mastery of Love (70)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)