PHP5 Class Inheritance Basics

I am going to write a tutorial which will help you to understand the concept of PHP5 class inheritance. If you want to get a solid grip on PHP5 OOP concepts, than i will recommend you to get help from my PHP5 Guide Book tutorials.

What is Inheritance?

Inheritance is the means by which one or more classes can be derived from a base class. A class that inherits from another is said to be a subclass of it.

This relationship is often described in terms of parents and children. A child class is derived from and inherits characteristics from the parent. These characteristics consist of both properties and methods.

Working with Inheritance: Let do it Simple 🙂

I am going to explain inheritance basics in a very simple, interesting and informative way. The first step in building an inheritance tree is to find the elements of the base class that don’t fit together or that need to be handled differently.

I am going to explain PHP5 Class Inheritance by keeping in mind “us” (I mean Human)

Final Classes

We will end up following three classes at the end of PHP5 Class Inheritance Basics.

  • Human Class: Parent/Base Class
  • Male: Child/Sub Class
  • Female: Child/Sub Class

Step1: Think a Little

Let’s think about some (For tutorial only) human characteristics.

  1. First Name
  2. Last Name
  3. Gender
  4. Skeleton

Their are (yes) several others characteristics of a human, but i am going to list 4 only for the sake of simplicity of inheritance tutorial.

Step 2: Find Common Attributes

Purpose of inheritance is reuse-ability and simplicity to the maximum extent. Let find similarities and differences in our human characteristics.

First Name:

First Name is a common attribute for all humans. All humans (Male/Female) have First Name. So i am going to mark it a common attribute.

Last Name:

It is same as First Name so mark it a common attribute as well.

Gender:

Gender can’t be common attribute for all humans. There are Males and Females in human race. So it can’t be declared a common attribute.

Skeleton:

Skeleton can’t be same for Males and Females. So it can’t also be declared a common attribute.

We end up at the following decision after Step 2.

  1. First Name: Common
  2. Last Name: Common
  3. Gender: Not Common
  4. Skeleton: Not Common

Step 3: Write Human Class (Base Class)

Let’s write our Human Class by considering Step 2. I am going to consider the following points while writing Human class.

  • Human Class will be our Base class.
  • Human Class will hold all human attributes in the form of Class Properties (either common or uncommon)
  • Human Class will contain those Class Methods which are for the manipulation of common properties only.

Let’s have a look into Human Class code snippet.

/**
* Inheritance Basics
* Class Human (Base/Parent Class)
*/

class Human {

	/**
	* Class Properties
	*/

	public $firstName;
	public $lastName;
	public $gender;
	public $skeleton;

	/**
	* Class Methods
	*/

	/**
	* Class Constructor
	*/

	public function __construct( $firstName = "First Name", $lastName = "Last Name" ) {

		$this->firstName = $firstName;
		$this->lastName = $lastName;

	}

	/**
	* Get Name
	*/

	public function getName() {
		$temp = "Name: " . $this->firstName . " " . $this->lastName . "<br />";
		return $temp;
	}

}

By keeping the above points in mind. I have written all four properties, and a single method (handling/manipulating common properties only) getName().

Step 4: Write Male/Female Class (Sub-Class) – Inheritance

Let’s write our Male/Female Class by considering the following points.

  • Male Class and Female Class will be Sub-Class of Human Class. So these will hold (inherit) all properties and methods of Human Class. This is the reason, why we have separated common and uncommon Human properties? This is re-usability and simplicity of our code.
  • We will not write any Class Properties in our Child Classes.
  • We will write methods in these classes which will manipulate uncommon properties.

Let’s have a look into code snippet.

Male Class:

/**
* Inheritance Basics
* Class Male (Subclass/Child Class)
*/

class Male extends Human {

	/**
	* Class Methods
	*/

	/**
	* Get Gender
	*/

	public function getGender() {
		$temp = "Gender: Male" . "<br />";
		$this->gender = $temp;
		return $this->gender;
	}

	/**
	* Get Skeleton
	*/

	public function getSkeleton() {

		$temp = "Mass: Skeletons are larger and heavier" . "<br />";
		$temp .= "Limbs: The humerus, ulna and radius, which are the major bones that comprise most of the arm, are thicker and longer." . "<br />";
		$temp .= "Skull: Jawbone or mandible is typically angular and square-shaped at the chin area." . "<br />";
		$temp .= "Pelvis: The coccyx or tailbone, which is the last bone of the spinal column, is less movable." . "<br />";

		$this->skeleton = $temp;
		return $this->skeleton;
	}

}

Female Class:

/**
* Inheritance Basics
* Class Female (Subclass/Child Class)
*/

class Female extends Human {

	/**
	* Class Methods
	*/

	/**
	* Get Gender
	*/

	public function getGender() {
		$temp = "Gender: Female" . "<br />";
		$this->gender = $temp;
		return $this->gender;
	}

	/**
	* Get Skeleton
	*/

	public function getSkeleton() {

		$temp = "Mass: Skeletons are not larger and heavier" . "<br />";
		$temp .= "Limbs: The humerus, ulna and radius, which are the major bones that comprise most of the arm, are not thicker and longer." . "<br />";
		$temp .= "Skull: Jawbone tends to be more rounded and pointed." . "<br />";
		$temp .= "Pelvis: The coccyx or tailbone, which is the last bone of the spinal column, is more movable." . "<br />";

		$this->skeleton = $temp;
		return $this->skeleton;
	}

}

Class Inheritance Keyword: extends

You have noticed that we inherit Parent Class by using the keyword “extends”.

Step 5: Class Instance

Let test our both Male and Female class. Code snippet is.

/**
* Male Object
*/

$male = new Male( "Mike", "John" );
echo $male->getName();
echo $male->getGender();
echo $male->getSkeleton();

echo "<hr />";

/**
* Female Object
*/

$female = new Female( "Martina", "John" );
echo $female->getName();
echo $female->getGender();
echo $female->getSkeleton();

Preview:

Name: Mike John
Gender: Male
Mass: Skeletons are larger and heavier
Limbs: The humerus, ulna and radius, which are the major bones that comprise most of the arm, are thicker and longer.
Skull: Jawbone or mandible is typically angular and square-shaped at the chin area.
Pelvis: The coccyx or tailbone, which is the last bone of the spinal column, is less movable.

Name: Martina John
Gender: Female
Mass: Skeletons are not larger and heavier
Limbs: The humerus, ulna and radius, which are the major bones that comprise most of the arm, are not thicker and longer.
Skull: Jawbone tends to be more rounded and pointed.
Pelvis: The coccyx or tailbone, which is the last bone of the spinal column, is more movable.

Conclusion: Very Important Points regarding to Inheritance

Important points to be considered at the end of tutorial,

  • Use differences as the basis for derived classes.
  • To create a child class, you must use the “extends” keyword in the class declaration.
  • Because the derived classes do not define constructors, the parent class’s constructor is automatically invoked when they are instantiated. (Learn about Constructors and Inheritance)
  • The child classes inherit access to all the parent’s public and protected methods (though not to private methods or properties).
  • Derived classes can extend but also alter the functionality of their parents.
  • By defining a class that extends another, you ensure that an object instantiated from it is defined by the characteristics of first the child and then the parent class. For Example.When I invoke $male->getName(), there is no such method to be found in the Male class, and the invocation falls through to the default implementation in Human.
  • The same is true of property accesses.

I hope you have enjoyed the PHP5 Class Inheritance tutorial. Dont Forget to Follow TutorialChip on Twitter or Subscribe to TutorialChip to Get the Latest Updates on Giveaways, Tutorials and More for Free.

2 thoughts

  1. Pingback: abcphp.com

Comments are closed.