PHP Interview questions


Total available count: 33
Subject - PHP Frameworks
Subsubject - PHP

Define inheritance and how it can be implemented in PHP?

A class can be inherited methods and properties of another class by using keyword extends in the class declaration and it is not possible to extend multiple classes; a class can only inherit from other base class.

The inherited methods and properties can be overridden by redeclaring them with the same name defined in the parent class. However, if a parent class has defined a method as final, that method can not be overridden. It is possible to access the overridden methods or static properties by referencing them with parent::.

When overriding methods, the parameter signature should remains same or PHP will generate an E_STRICT level error. This does’nt apply to a constructor, which allows overriding with different parameters.

Example: 

<?php
class ExtendClass1 extends SimpleClass1
{
   // Redefine the parent method
   function displayVar()
   {
      echo "Slightbook\n";
      parent::displayVar();
   }
}

$extended = new ExtendClass1();
$extended->displayVar();
?>

 




Next 5 interview question(s)

1
What are Objects?
2
What are Classes?
3
What is an operator and how many types of operators are there?
4
What is the difference between named functions vs anonymous functions?
5
What is the difference between var_dump() vs print_r()?