php oops concepts
Polymorphism
Polymorphism means many forms, in PHP it means that deciding which function should be called in runtime. In PHP polymorphism means inheritance and overriding functions.
e.g.
<?php
class ParentC
{
public function func1()
{
echo ‘<br>Parent class’;
}
}
class ChildC extends ParentC
{
public function func1()
{
echo ‘<br>Child class’;
}
}
class Poly
{
public function callFunc(ParentC $obj)
{
$obj->func1();
}
}
$childObj=new ChildC();
$polyObj=new Poly();
$polyObj->callFunc($childObj);
?>
PHP does not support compile time polymorphism, so it does not support operator overloading and function overloading. Function overloading is not supported in PHP because in PHP we can not have two functions with the same name.
If we have any requirement like a function can have some optional parameters then we use func_get_arg() to get the parameters passed to the function.
so if we have a function say func1($arg1,$arg2){……} , then:-
func1(‘a’,'b’); //is valid
func1(‘a’,'b’,'c’,'d’); //is also valid
func1(‘a’); //is invalid as the function requires at least two parameters
Encapsulation
Encapsulation is a technique for data hiding. Sometimes we have some private variables in the class so that they should not be modified/accessed from out side of the class and we do provide some public or protected functions in out class to use those variables in the way they are intended to be used, then this technique is called Encapsulation.
e.g.
<?php
class EncapExp
{
private $num1;
private $num2;
public function setNumbers($n1,$n2)
{
if(is_int($n1))
{
$this->num1=$n1;
}
else
{
echo ‘<br>’.$n1.’ is not a number’; exit;
}
if(is_int($n2))
{
$this->num2=$n2;
}
else
{
echo ‘<br>’.$n2.’ is not a number’; exit;
}
}
public function addNumbers()
{
return $this->num1+$this->num2;
}
}
$obj=new EncapExp();
$obj->setNumbers(2,3);
echo $obj->addNumbers(); //output: 5
$obj->setNumbers(2,’a'); // outpt: a is not a number
?>
so in the above example we can see that we are restricting the invalid (non-integer) values to assign to the variables. If they would be public then one could assign non-integer values to them.
Inheritance
Objects of one class can acquire the properties of objects of other class using inheritance.
PHP doesn’t support multiple inheritance. So unlike JAVA we cant implement more than one interface.
Abstract Class
- Its not compulsory that an abstract class will have at least one abstract method or at least one non-abstract method
- e.g. abstract class abs1{ public function fun1(){ echo “hi”; } } is ok
- abstract class abs2{ abstract function fun2(); } is ok
- abstract class abs3{ public function f1(){…..} abstract protected function f2(); } is also ok
- Abstract function can not be private
Interfaces
- Interfaces can not have protected or private declarations
- Interfaces can not have member variables
interface intFace1()
{
var $var1;
public function intFunct1();
}
will show an error like “Interfaces may not include member variables”
interface intFace1
{
protected function func1();
private function func2();
}
This will show error like : Access type for interface method intFace1::func1() must be omitted
An interface can extends other interface
<?php
interface intFace1
{
public function func1();
}
interface intFace2 extends intFace1
{
public function func2();
}
class testA implements intFace2
{
public function func1()
{
echo ‘<br>func1′;
}
public function func2()
{
echo ‘<br>func2′;
}
}
$obj=new testA();
$obj->func1();
$obj->func2();
?>
An abstract class can also implement other interface
<?php
interface intFace1
{
public function func1();
}
abstract class absClass implements intFace1
{
public function func1()
{
echo ‘<br>func1 of abstract class ‘;
}
}
class testB extends absClass
{
}
$obj=new testB();
$obj->func1();
?>
An Abstract class can also extends other Abstract class
<?php
abstract class abs1
{
abstract function fun1();
}
abstract class abs2 extends abs1
{
abstract function fun2();
}
class testA extends abs1
{
function fun2()
{
echo ‘<br>fun2 of testA class’;
}
function fun1()
{
echo ‘<br>fun1 of testA class’;
}
}
$obj=new testA();
$obj->fun2();
$obj->fun1();
?>