Showing posts with label php oops. Show all posts
Showing posts with label php oops. Show all posts

Friday, February 21, 2014

PHP oops Method overriding Example



class Processes
{
	public function process1()
	{
		echo  'I am process 1';
	}

      public function process2()
	{
		echo  'I am process 2';
	}

	public function process3()
	{
		echo  'I am process 3';
	}

      public function startProcess()
	{
		$this->process1();
		$this->process2();
		$this->process3();
	}

}


class ChildProcess extends Processes
{

	public function process2()
	{
		echo  'I am overrided process 2';
	}


}

$process = new Processes();

$process->startProcess();

/* output is

I am process 1
I am process 2
I am process 3

*/


$child = new ChildProcess();
$child->startProcess();

/*
output is 

I am process 1
I am overrided process 2
I am process 3

*/

From the above example,

Processes is a parent class having 4 methods.  method startProcess() display other three methods content.

if we want to override any of the methods we should create a child class and extend which method we want to override.

in the example process2 overrided by child class.

when this line executes $child->startProcess(); , compiler first execute process1 from parent and execute overrided method

pocess2 then control goes to process3.

PHP oops Hybrid Inheritance Example



class GrandFather
{
	public function pocketMoney()
	{
		return  'Take this 1000 $';
	}

}


class Father extends GrandFather
{
	public function myIncome()
	{
		echo "My father told ".parent::pocketMoney();
	}
	
	public function myExpense()
	{
		return 'Take this 500 $
'; } } class Elder extends Father { public function myEarning() { echo "Father told me ".parent::myExpense(); } } class Younger extends Father { public function myEarning() { echo "Father told me ".parent::myExpense(); } } $elder = new Elder(); $elder->myEarning(); // output Father told me Take this 500 $ $younger = new Younger(); $younger->myEarning(); // output Father told me Take this 500 $

PHP oops Hierarchical Inheritance Example




class Father
{
	public function pocketMoney()
	{
		return  'Take this 500 $';
	}

}

class Elder extends Father
{
		
	public function myEarning()
	{
		echo "Father told me ".parent::pocketMoney();

	}

}

class Younger extends Father
{
	public function myEarning()
	{
		echo "Father told me ".parent::pocketMoney();

	}

	
}


$elder = new Elder();

$elder->myEarning();  // output Father told me Take this 500 $

$younger = new Younger();

$younger->myEarning();  // output Father told me Take this 500 $


PHP oops Multilevel Inheritance Example





class GrandFather
{
	public function gfAge()
	{
		return  ' age is 80';
	}

}

class Father extends GrandFather
{
		
	public function fAge()
	{
		return  ' age is 50';
	}

}

class Son extends Father
{
	public function sAge()
	{
		return  'age is 20';
	}

     public function myHistory()
     {
	 echo "my grand father ".parent::gfAge();
	 echo "my father ".parent::fAge();
       echo "my ".$this->sAge();
     }
	
}


$son = new Son();

$son->myHistory();

// output is

//my grand father age is 80
//my father age is 50
//my age is 20

Above code shows simple example for multi level inheritance,

class GrandFather is a base class , extended by Father class and this is extended by

Son class

myHistory function in Son class access Father class and GrandFather class functions

here the class hieararchy is,


GrandFather
    |
    | 
  Father
    |
    |
   Son 

 

PHP Call parent class function inside child class function example




class Person
{
	
	public function whoAmi()
	{
		return 'I am a person';
	}
	
}

class SimplePerson extends Person
{
	
	public function personEnquiry()
	{
		$whoami = parent::whoAmi();
		echo "The person told ".$whoami;
	}
	
}



$person = new SimplePerson();

$person->personEnquiry();

// output is The person told I am a person

In the above example class Person have a function named whoAmi().

we want to call it inside another class that extends person class.

by using parent:: keyword we can access function from its parent class

parent::whoAmi(); line access the whoAmi function that is defined in the Parent class.

PHP oops Single inheritance Example



class Processes
{
	
	public function process1()
	{
		echo  'process 1 is completed';
	}

	public function process2()
	{
		echo  'process 2 is completed';
	}

	
}

class ChildProcess extends Processes
{
	
	public function process3()
	{
		echo  'process 3 is completed';
	}
	
}



$process = new Processes();

$process->process1(); // output is  process 1 is completed


$process->process2(); // output is process 2 is completed


// access parent class methods

$cprocess = new ChildProcess();

$cprocess->process1(); // output is  process 1 is completed


$cprocess->process2(); // output is process 2 is completed

$cprocess->process3(); // output is process 3 is completed


In the above example we have a class Processes with two methods

the class ChildProcess extends the Processes class.
 
suppose we want to complete a task that should 3 process 

levels then using inheritance,

not necessary to define already defined process1, process2. 

directly we can extend a parent

class and create a child one. Inheritance is very helpful 

to reduce code duplicating. Inheritance is

very simple to use and yet powerfull. 

PHP oops calling a method from another mehod of a class example

class Person
{
	
	public function function1()
	{
		return 'from function1';
	}


      public function function2()
	{
		$function1 = $this->function1();

		echo $function2 = $function1." from function2";
	}
	
}

$person = new Person();

$person->function2();

In the above example class Person have a function named function1.
 we want to call it inside another function named fnction2.
 function1 return a string. by using $this keyword we can access function from a class
 $function1 = $this->function1(); this line access function1 and store the returned
 string "from function1" into $function1 variable;

PHP oops Static function example



class Person
{
	
	public static function statExample()
	{
	   echo 'Hi static  function. no need to instantiate';
	}
	
}



Person::statExample();

Explanation:

Static functions are function that starts with static keyword.

No need to instantiate static functions

Performance methods implemented as static and
instance would little difference in terms of
performance 

The short answer is that static functions are faster.


Tuesday, February 18, 2014

PHP Access Modifiers - 3

class Person
{
 public function pub()
 {
  echo 'I am Publicly accessible<br>';
 }

 private function pri()
 {
  echo 'I am Private function<br>';
 }
 protected function pro()
 {
  echo 'I am Protected function<br>';
 }
}



class NewPerson extends Person
{

 public function getPrivatedata()
 {
  parent::pri();
 }

 public function getProtectedata()
 {
  parent::pro();
 }

}



$newperson = new NewPerson;



//$newperson->getPrivatedata();  // Fatal error: Call to private method Person::pri() from context 'NewPerson'



$newperson->getProtectedata();  // Output is I am Protected function




from the above example newperson object trying to access a function , that is defined as private from its class Person. that shows error message. because we cant access it from derived class.

after that we call a function getProtectedata() . it access protected function pro from Person. It can be accessible , because of protected.

so whenever we want to access a function from its child class, it should be defined as protected, else private.

PHP - Access modifiers - 2

class Person
{

 public function pub()
 {
  echo 'I am Publicly accessible<br>';
 }

 private function pri()
 {
  echo 'I am Private function<br>';
 }

 protected function pro()
 {
  echo 'I am Protected function<br>';
 }
}

$person = new Person;



$person->pub();  // output is I am Publicly accessible

$person->pri();  // Fatal error: Call to private method Person::pri()

$person->pro();  // Fatal error: Call to protected method Person::pro()