object oriented Inheritance in php

We can use Objected Oriented Programming model in PHP which has rich set of features. Here an example of inheritance in PHP is presented:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

</head>

<body>
<?php
class ParentClass
{
  function ShowInParent()
  {
    echo "This is in Parent Class<br>";
  }
}
class MyClass extends ParentClass
{
  function ShowInChild()
  {
    echo "This is in child class<br>";
  }
}
$a = new MyClass;
echo "CASE 1:" . $a instanceof MyClass;
echo "<br>";
echo "CASE 2:" . $a instanceof ParentClass;
echo "<br><br>";

$a -> ShowInParent();
$a -> ShowInChild();
?>
</body>
</html>

Here is another example of inheritance in php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

</head>

<body>
<?php
class MyParentClass
{
  function ParentClassFunction()
  {
  echo "THis is parent class";	
  }	
}

class MyChildClass extends MyParentClass
{
  function ChildClassFunction()
  {
    echo "<br>This is child class<br>";
  }
}


$a = new MyParentClass;
$b = new MyChildClass;
$b -> ParentClassFunction();
$b -> ChildClassFunction();
?>
</body>
</html>

 

Add a Comment

Your email address will not be published. Required fields are marked *