Oops in PHP interview questions

Oops in PHP interview questions

What is Object Oriented Programs (OOP) and How We can relate All these by Programs.
Object means in real life Sun, Moon, House, Vehicle, Computer,Books, Home appliance, etc. Here let an example of house is made of by Bricks, Sand, Iron Rod, and Cement as these all are object, so taking all to a make a house. Same way Software made of different Object.

In Software Object Oriented Programs (OOP) are having different terms. Before go to interview OOPs in PHP interview questions answers you should first to gain some knowledge regarding OOps.

  • Class
  • Object
  • Member Variable
  • Member Funtion
  • Inheritance
  • Keywords(Public,Private,protected,etc)
  • Encapsulation
  • Constructor
  • Destructor
  • Overloading
  • Parent Class
  • Child Class
  • Data Abstraction
  • Polymorphism
  • Interface
  • Constants

So If we know all those thoroughly their behaviour and uses then we can use that Oops concept in our program.
Then Why you will use object Oriented Concept in your Program. What is advantages of Oops in PHP.

  • Code Readable
  • Code Reusable
  • Code Structured
  • Time Saving
  • Better performance

1. What is class concept oop in php?

Class is programmer defined data type which is consist of properties and methods.
It’s a blueprint of object.
It define the properties or functionality of an Object.

Example of Class in PHP, Here you need create a class, name should be same as file name.
[SyntaxHL]<?php
class Laptop{
public function Brand(){
echo “Lenovo”;
}
public function price(){
echo “Rs 34000.00”;
}
}
$obj = new Laptop();
$obj->Brand();
$obj->price();
?>

//Output
Lenovo
Rs 34000.00
[/SyntaxHL]

What is object in oop in php?

  1. Object is a instance of a class.
  2. An object can instantiate, but a class can’t instantiate.
  3. new  is a keyword to Instantiate an object of a class.

Above an example you can seen the class name Laptop and object is created by using keyword new. see the example below.
[SyntaxHL]$obj = new Laptop();[/SyntaxHL]

What are the basic difference between class and object in PHP

Class-
1. Class is a blueprint of object.
2. A class can’t instantiate.
4. Class is generic.
Object-
1. Object is instance of class.
2. An object can instantiate.
4. Object is specific.

What is inheritance in oop PHP

If child class can access the methods or variables of it’s parent class then you can call that is inheritance.However the parent class property or methods should be public or protected,so it can inherited.
Here PHP supports single level inheritance and multilevel inheritance but not supports multiple inheritance.
Inheritance in oop PHP done by keywords. extends

[SyntaxHL]<?php

class A{

function Developer(){
echo “PHP Developer”;
}
}

class B extends A{
}

$b= new B();
$b->Developer(); // PHP Developer.

?>[/SyntaxHL]

Why multiple inheritance not supported in PHP and how we can achieve this?

Multiple inheritance suffers from the Diamond Problem, which has not been solved in PHP yet, so to overcome it PHP provides interface.By using multiple interface we can achieve same as multiple inheritance.

What is interface explain with example?

1. Interfaces contain no data variables, only function prototypes.
2. Abstract classes and interface both are same but only difference is abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.
3. Interface you can only defined your methods without body.
4. It can created by using keyword interface.
5. It used in a class for simulate multiple inheritance the keyword implements.
6. A class can implement multiple interface.
[SyntaxHL]Examples-
interface Interface_name{
public some_function();
}
Class A implements Interface_name{
public some_function(){
// Body must be here.
echo “I am a PHP developer.”;
}
$b= new A();
$b->some_function(); // Output : I am a PHp developer.
[/SyntaxHL]
Example of implementation of multiple interface in one single class.
[SyntaxHL]interface One { }
interface Two { }
interface Three { }
//A class can implements multiple interfaces
class A implements One, Two, Three { }[/SyntaxHL]

What is difference between Abstract class and Interface?

Interface:
1. Interface supports Multiple inheritance.
2. Interface methods does not contain any body.
3. Interface does not contain any data member.
4. All methods of an interface must be defined as public.
Abstract Class:
1. Abstract class never support multiple inheritance.
2. Abstract class methods contains body.
3. abstract class contains data member.
4. Methods and members of an abstract class can be defined with any visibility.

What is access modifier?

It’s a very important features in object oriented program, it provides visibility of methods or property of a class. There are 3 types of access modifiers available. these are follows below:
1.Private.
2.Protected.
3.Public.

What is use of final keyword in php oop?

Final is a keyword which prevents child classes from overriding a method. If you pre-fix the final keyword before a class it can’t be extendable by other class.

What is Concept of Abstraction in PHP?

Abstraction is a key features in PHP, in which you can restrict a class for create an object. If you pre-fix this keywords abstract then it can’t create an instances.
[SyntaxHL] abstract class A{
// Body
}[/SyntaxHL]

In above class A, you can not able to create a object or instance but you only extend it by using keyword. extends.

Leave a Reply

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