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.
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).
<?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.
<?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.
<?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.
parent::methodName()- Example
<?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.
<?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.
<table><tr> <th>Title 1</th> <th>Title 2</th> </tr></table>
.some_class { line-height: 150%; }
document.getElementById("id_button").onclick = function(){ window.open("http://coursesweb.net/"); }
$ar_dir = scandir("dir_name"); var_export($ar_dir);