Php-mysql Course

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.


The abstract methods are declared with the abstract keyword, and cannot have an implementation, they simply declare the method's signature.
abstract public function methodName($arguments);
Abstract methods are only created in an abstract class.

  - Example:
In this example is created an abstract class with a property ($name), an abstract method ( greetName() ) and a normal method ( setName() ).
<?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:
Fatal error: Cannot instantiate abstract class AbstractClass in ...
As you can see in the example above, the abstract method, greetName() does not have an implementation code, but, by declarating this method abstract, you ensure that it must be defined by all child class derived from AbstractClass.
The abstract methods declared in the parent class must be defined in the child classes with the same (or a less restricted) attribute of access, and the same number of arguments.

In the next example we create a child class derived from AbstractClass. This subclass must contain the greetName() method becouse it is marked abstract in the parent class.
<?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:
Fatal error: Class ChildClas contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (AbstractClass::greetName) in ...

Child classes and their instances can uses the public and protected elements created in the abstract parent class.
For example:
<?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:
Welcome MarPlo
Today: Saturday, 2-4-2011

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 - Abstract classes

Last accessed pages

  1. Mysql SELECT JOIN tables on two different Databases (4498)
  2. jQuery UI draggable - Drag elements (11448)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142520)
  4. Using the Bone Tool (4253)
  5. Node.js Move and Copy Directory (20134)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)