“CLASS ABSTRACTION IN PHP”

//WHEN I READ THE PHP MANUAL THESE ARE MY FINDINGS
//JUST COMMENT IF I DO IT RIGHT OR WRONG I AM OPEN
//FOR SUGGESTIONS.

//TOPIC: CLASS ABSTRACTION

/* ACCORDING TO PHP 5 MANUAL!!

PHP 5 introduces abstract classes and methods.
It is not allowed to create an instance of a
class that has been defined as abstract.

//MEANING YOU CANNOT INSTANTIATE THE ABSTRACT CLASS OR DECLARE AS NEW eg. “$F = new Father;” WRONG!

Any class that contains at least one abstract
method must also be abstract.

//NATURALLY IF YOU ARE NOT ABSTRACT CLASS DO NOT DECLARE ABSTRACT METHOD

Methods defined as abstract simply declare the method’s
signature they cannot define the implementation.

//YOU CAN CALL THE METHODS CREATED BY YOUR CHILD BUT YOU AS A PARENT YOU DO NOT HAVE THE IMPLEMENTATION OF IT.

When inheriting from an abstract class, all
methods marked abstract in the parent’s class
declaration must be defined by the child;

//CORRECT!! FORCING YOU TO USE THOSE DECLARATION BY THE PARENT OR ABSTRACT CLASS

additionally, these methods must be defined
with the same (or a less restricted) visibility.

//CORRECT YOU SHOULD UNDERSTAND THE 3P’S OF VISIBILITY THE PUBLIC, PROTECTED AND PRIVATE (i will explain these in my next tutorial)

For example, if the abstract method is defined as protected,
the function implementation must be defined as either
protected or public, but not private.
*/

//EXAMPLE LET US MAKE A HISTORY OF FATHER AND HIS 3 SONS

//THE FATHER LOVE HIS 3 SONS HE WANTS TO DISCIPLINE THEM
//HE FORCED HIS 3 SONS AFTER BUYING THE FOOD THEY SHOULD PAY IT.
//HE GAVE THE RULES / ABSTRACT METHODS WITH HIS SIGNATURE.
//IF ONE OF HIS SONS DISOBEY HIS ABSTRACT OR COMMAND ALL OF THEM WILL DIE (FATAL ERROR/CRASH).

abstract class Father {

		//RESULTS IF THEY OBEY OR NOT
		protected $results; 

		//The father FORCING his 3 sons to do these if not all of them will die
		abstract protected function buy_food();
		abstract protected function pay_food();

		//COMMON METHODS THAT FATHER SHARED TO HIS 3 SONS THAT THEY HAVE THE POWER TO
		//USE HIS INVESTIGATION POWER BUT THE IMPLEMENTATION IS BELONGS TO HIS 3 SONS.
		public function Father_Investigate() {
				$this->results = '<br />The '.get_class($this).' buy '.$this->buy_food().' for the amount of '.$this->pay_food().' dollars.<br />';
				return $this->results;
		}
}

//IF THE SONS DID NOT FOLLOW WHAT THE ABSTRACT/FORCING METHODS DECLARE BY THE FATHER IT WILL CAUSE FATAL ERROR
//ALL OF THEM DIED SO THEY HAVE TO FOLLOW THEIR FATHER INSTRUCTION EXACTLY.
//THEY ARE SONS OF HIS FATHER SO THEY INHERIT THE FATHERS BLOOD 🙂
class FirstSon extends Father {
		//BUY
		public function buy_food() {
			return "Chicken";
		}
		//PAY
		public function pay_food() {
			return "$10.00";
		}
}

class SecondSon extends Father {
		//BUY protected visibility
		protected function buy_food() {
			return "Spaghetti";
		}
		//PAY
		public function pay_food() {
			return "$15.00";
		}
}

class ThirdSon extends Father {
		//BUY
		public function buy_food() {
			return "Hamburger";
		}
		//PAY
		public function pay_food() {
			return "$20.00";
		}
}

//THE BUY AND PAY ARE THE ABSTRACT SIGNATURE/METHODS THAT ALL OF THE SONS WILL BE IMPLEMENTED INSIDE THEMSELVES.
//SHARING TO THEIR FATHER COMMON METHODS

$First = new FirstSon;
echo $First->Father_Investigate();

$Second = new SecondSon;
echo $Second->Father_Investigate();

$Third = new ThirdSon;
echo $Third->Father_Investigate();

//OUTPUT: get_class($this) you can use this to show the class who extends the father the name of HIS 3 SONS.

/*
The FirstSon buy Chicken for the amount of $10.00 dollars.

The SecondSon buy Spaghetti for the amount of $15.00 dollars.

The ThirdSon buy Hamburger for the amount of $20.00 dollars.
*/

//OKAY GUYS TRY TO EXPERIMENT NOW AND DISOBEY YOUR FATHER WHAT WILL HAPPEN TO THAT FAMILY 🙂

EXAMPLE: DELETE ONE TASK INSIDE THE SON TRY TO REMOVE PAY pay_food() JUST LEAVE THE BUY OR buy_food();.


class FirstSon extends Father {
		//BUY
		public function buy_food() {
			return "Chicken";
		}
}

//AND ALSO TRY TO ADD ANOTHER FORCE DISCIPLINE OR ACTION FROM THE FATHER AND WHAT WILL HAPPEN TO THAT FAMILY :-).

EXAMPLE:


abstract protected function buy_food();
abstract protected function pay_food();

//ADDITIONAL COMMAND OR ABSTRACT BY THE FATHER
abstract protected function go_home();

ANOTHER SCENARIO:

ONCE THE 3 SONS COMPLETED WHAT THEIR FATHER INSTRUCTION THEY CAN DO ANY OTHER THINGS NOT IN ABSTRACT/INSTRUCTED BY THE FATHER.


class FirstSon extends Father {
 //BUY
 public function buy_food() {
 return "Chicken";
 }
 //PAY
 public function pay_food() {
 return "$10.00";
 }

<strong> //DO ANOTHER WORK NOT GIVEN BY THE FATHER</strong>
 public function FirstSonDoAnotherWork() {
 return "Sleep.";
 }
}
class SecondSon extends Father {
 //BUY protected visibility
 protected function buy_food() {
 return "Spaghetti";
 }
 //PAY
 public function pay_food() {
 return "$15.00";
 }
 //<strong>DO ANOTHER WORK NOT GIVEN BY THE FATHER</strong>
 public function SecondSonDoAnotherWork() {
 return "Watching TV.";
 }

}

class ThirdSon extends Father {
 //BUY
 public function buy_food() {
 return "Hamburger";
 }
 //PAY
 public function pay_food() {
 return "$20.00";
 }
<strong> //DO ANOTHER WORK NOT GIVEN BY THE FATHER </strong>
 public function ThirdSonDoAnotherWork() {
 return "Play Mobile Games.";
 }

}

//THE BUY AND PAY ARE THE ABSTRACT SIGNATURE/METHODS THAT ALL OF THE SONS WILL BE IMPLEMENTED INSIDE THEMSELVES.
//SHARING TO THEIR FATHER COMMON METHODS

$First = new FirstSon;
echo $First->Father_Investigate();
echo $First->FirstSonDoAnotherWork();

$Second = new SecondSon;
echo $Second->Father_Investigate();
echo $Second->SecondSonDoAnotherWork();

$Third = new ThirdSon;
echo $Third->Father_Investigate();
echo $Third->ThirdSonDoAnotherWork();

OUTPUT:

The FirstSon buy Chicken for the amount of $10.00 dollars.
Sleep.

The SecondSon buy Spaghetti for the amount of $15.00 dollars.
Watching TV.

The ThirdSon buy Hamburger for the amount of $20.00 dollars.
Play Mobile Games

//AND THEY LIVE HAPPILY HAPPILY EVER AFTER….THE END…:-)

Advertisements
This entry was posted in PHP and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s