Thursday, 20 July 2017

OOPS interview questions and answers in php


1
.What is Polymorphism ?

Ans: Polymorphism means the ability to have many forms.The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they're all used the same way
        
There are two types of Polymorphism; they are: 
  1. Compile time (function overloading) or static polymorphism
  2. Run time (function overriding) or dynamic polymorphism
Usually the  PHP "does not support" compile time polymorphism as per Native langues but by using some special cases we can achieve Static polymorphism , 
 Ref:- http://swapnilg.com/overloading-php-example

2.What is function overloading and give an example in php?
 Ans: Function overloading is the ability to create multiple functions of the same name with different implementations.Function overloading in PHP is used to dynamically create properties and methods. These dynamic entities are processed by magic methods which can be used in a class for various action types. Function overloading contains same function name and that function preforms different task according to number of arguments.
 Types of Overloading in PHP: There are two types of overloading in PHP.
  • Property Overloading
  • Method Overloading
  We can achieve property overloading using below methods. 
  • _set(): triggered while initializing overloaded properties.
  • __get(): triggered while using overloaded properties with PHP print statements.
  • __isset(): This magic method is invoked when we check overloaded properties with isset() function
  • __unset(): Similarly, this function will be invoked on using PHP unset() for overloaded properties.
   Ex:
  
<?php
class GFG { 
// Location of overloading data 
private $data = array(); 
// Overloading not used here 
public $declared = 1; 
// Overloading used when accessed 
// outside the class 
private $hidden = 2; 
// Function definition 
public function __set($name, $value) { 
echo "Setting '$name' to '$value'\n"; 
$this->data[$name] = $value; 
// Function definition 
public function __get($name) { 
echo "Getting '$name: "; 
if (array_key_exists($name, $this->data)) { 
return $this->data[$name]; 
$trace = debug_backtrace(); 
return null; 
// Function definition 
public function __isset($name) { 
echo "Is '$name' set?\n"; 
return isset($this->data[$name]); 
// Definition of __unset function 
public function __unset($name) { 
echo "Unsetting '$name'\n"; 
unset($this->data[$name]); 
// getHidden functino definition 
public function getHidden() { 
return $this->hidden; 
// Create an object 
$obj = new GFG; 
// Set value 1 to the object variable 
$obj->a = 1; 
echo $obj->a . "\n"; 
// Use isset function to check 
// 'a' is set or not 
var_dump(isset($obj->a)); 
// Unset 'a' 
unset($obj->a); 
var_dump(isset($obj->a)); 
echo $obj->declared . "\n\n"; 
echo "Private property are visible inside the class "; 
echo $obj->getHidden() . "\n\n"; 

echo "Private property are not visible outside of class\n"; 
echo $obj->hidden . "\n"; 

?> 
Out Put :
       Setting 'a' to '1'
Getting 'a: 1
Is 'a' set?
bool(true)
Unsetting 'a'
Is 'a' set?
bool(false)
1

Private property are visible inside the class 2

Private property are not visible outside of class
Getting 'hidden:
 
Method Overloading: It is a type of overloading for creating dynamic methods that are not declared within the class scope. PHP method overloading also triggers magic methods dedicated to the appropriate purpose. Unlike property overloading, PHP method overloading allows function call on both object and static context.
The related magic functions are,
  • __call() – triggered while invoking overloaded methods in the object context.
  • __callStatic() – triggered while invoking overloaded methods in static context.
EX :-    
 <?php 
class GFG { 
public function __call($name, $arguments) { 
echo "Calling object method '$name' "
. implode(', ', $arguments). "\n"; 
public static function __callStatic($name, $arguments) { 
echo "Calling static method '$name' "
. implode(', ', $arguments). "\n"; 
// Create new object 
$obj = new GFG; 
$obj->runTest('in object context'); 
GFG::runTest('in static context'); 
?> 
Output:
Calling object method 'runTest' in object context
Calling static method 'runTest' in static context

3. Can you  call parent class constructor using clild class object ?


An
Parent constructors are not called implicitly if the child class defines a constructor. 
In order to run a parent constructor, a call to parent::__construct() within
 the child constructor is required.

ex :  
abstract class view
{
 public $vieverid;

 function __construct($viewerid) {
  $this->viewer = $viewerid;
 }
}
class viewactor extends view{

 function __construct($viewerid) {
  parent::__construct($viewerid); // manual call
  // do your stuff here...
  $this->viewerid = $viewerid;
 }

3.Interface Vs abstract class ?

Image result


Abstrsct Class Properties 


Abstract Class
1. Contains an abstract method
2. Cannot be directly initialized
3. Cannot create an object of abstract class
4. Only used for inheritance purposes
5.Abstract Class is not possible to implement multiple inheritance.
Abstract Method
1. Cannot contain a body
2. Cannot be defined as private
3. Child classes must define the methods declared in abstract class

  Abstract class doesn't support multiple inheritance:Abstract class can extends another abstract class,Abstract class can provide the implementation of interface.But it doesn't support multiple inheritance.
interface MyInterface{
    public function foo();
    public function bar();
}

abstract class MyAbstract1{
    abstract public function baz();
}


abstract class MyAbstract2 extends MyAbstract1 implements MyInterface{
    public function foo(){ echo "foo"; }
    public function bar(){ echo "bar"; }
    public function baz(){ echo "baz"; }
}

class MyClass extends MyAbstract2{
}

$obj=new MyClass;
$obj->foo();
$obj->bar();
$obj->baz();
//output: foobarbaz
Can not instantiate abstract class: Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract.
Example below :
abstract class AbstractClass
{

    abstract protected function getValue();
    abstract protected function prefixValue($prefix);


    public function printOut() {
        echo "Hello how are you?";
    }
}

$obj=new AbstractClass();
$obj->printOut();
//Fatal error: Cannot instantiate abstract class AbstractClass
Ref : http://www.developer.com/lang/php/article.php/10941_3604111_2/PHP-5-OOP-Interfaces-Abstract-Classes-and-the-Adapter-Pattern.htm

4. What is Data Abstraction ?
ANS : Data abstraction refers to providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.

5.What is 
Encapsulation ?

ANS:
          Encapsulation is the process of combining data and functions into a single unit called class. In Encapsulation, the data is not accessed directly; it is accessed through the functions present inside the class. In simpler words, attributes of the class are kept private and public getter and setter methods are provided to manipulate these attributes. Thus, encapsulation makes the concept of data hiding possible.
More Details ; http://www.csharpstar.com/top-30-oops-interview-questions/

No comments:

Post a Comment