Inheritance in php

Inheritance is a well-established programming principle, and PHP makes use of this principle in its object model. This principle will affect the way many classes and objects relate to one another.
For example, when you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.
This is useful for defining and abstracting functionality, and permits the implementation of additional functionality in similar objects without the need to reimplement all of the shared functionality.


$object = new Subscriber ();
$object->name = "abcd";
$object->password = "password";
$object->phone = "017 23 6532";
$object->email = "";
$object->display ();
class User {
	public $name, $password;
	function save_user() {
		echo "Save User code goes here";
class Subscriber extends User {
	public $phone, $email;
	function display() {
		echo "Name: " . $this->name . "<br />";
		echo "Pass: " . $this->password . "<br />";
		echo "Phone: " . $this->phone . "<br />";
		echo "Email: " . $this->email;

The original User class has two properties, $name and $password, and a method to save the current user to the database. Subscriber extends this class by adding an additional two properties, $phone and $email, and includes a method of displaying the properties of the current object using the variable $this, which refers to the current values of the object being accessed.


Name: abcd
Pass: password
Phone: 017 23 6532

You may also like...