Abstract Method and Classes in PHP


Abstract method and classes:


An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class. Abstract Class contains only declare the method's signature, they can't define the implementation. Abstraction class are defined using the keyword abstract .


Abstract class is a base class

Abstract class u cannot create object

You cannot create  instance of this abstract class. Abstract class is the base class


<?php abstract class animal

{

function testing()

{

echo "Call testing function";

}

}

class subanimal extends animal

{

function horse()

{

return "call horse function";

}

}

$animal=new subanimal();

$animal->testing();

echo "<br>";

echo $animal->horse();

?>

Output:

Calltestingfunction
call horse function

Another example:


<?php abstract class animal

{

function testing()

{

echo "Call testing function";

}

function tot($a,$b)

{

echo $a+$b;

}

}

class subanimal extends animal

{

function horse()

{

return "call horse function";

}

function tot($a,$b)

{

echo $a*$b;

//echo parent::tot($a,$b);

}

}

$animal=new subanimal();

$animal->testing();

echo "<br>";

echo $animal->horse();

echo "<br>";

$animal->tot(5,4);

?>


Output: 20  For using Parent output is 9.

Custom Search

%d bloggers like this: