The abstract classes and methods are used to create a model of minimum required methods which must be defined in normal sub-classes derived from an abstract class (with extends).
An abstract class is created with the abstract keyword.
An abstract class cannot be instantiated, can only be inherited by other sub-classes extended from it.
abstract public function methodName($arguments);Abstract methods are only created in an abstract class.
<?php // AbstractClass class abstract class AbstractClass { protected $name; // declare an abstract method abstract public function greetName($greet); // define a normal method public function setName($name) { $this->name = $name; // sets the value of $name property } } ?>Any attempt to create an object instance of the AbstractClass will cause an error like this:
<?php // ... include AbstractClass code // a child class derived from AbstractClass class ChildClas extends AbstractClass { // the required method public function greetName($greet) { return $greet. ' '. $this->name; } // another method, returns today's date public function todayDate() { return 'Today: '. date('l, j-n-Y'); } } ?>If we were to create a child class derived from AbstractClass that does not implement the greetName() method, will generate an error like this:
<?php // ... include ChildClas code // object instance of ChildClas $obj = new AbstractClass(); // calls setName(), defined in AbstractClass $obj->setName('MarPlo'); // access the methods defined in ChildClas class echo $obj->greetName('Welcome'); echo '<br />'. $obj->todayDate(); ?>Will display:
<meta name="description" content="70-160 characters that describes the content of the page" />
#some_id { clear: both; }
var divs = document.getElementsByTagName("div"); var nr_divs = divs.length; alert(nr_divs);
$arr =[7, 8, "abc", 10); $nri = count($arr); echo $nri; // 4