Dear Reader, here we are going to make you understood about “OOPS PHP interview questions and answers” which we watch out that the Interviewer wants and I am sure these questions and answer will be very important for you because here I am mention mostly asked questions and answer, lets start.
Q 1:- What is Object Oriented Programming?
Answer– Object-oriented programming (OOP) is a programming language model organized around objects rather than actions;
Objects are instances of classes, are used to interact with one another.
Following are few examples of object-oriented programming languages
PHP, C++, Objective-C, Smalltalk, C#, Perl, Python, Ruby.
The goals of object-oriented programming are:
- Increased understanding.
- Ease of maintenance.
- Ease of evolution.
Q 2:- What is data modeling?
Answer- In class, we create multiple get/set function to get and set the data through the protected functions known as Data Modeling
1 2 3 4 5 |
class webpreparationsModel { public function __set( $key, $value ) { $this->$key = $value; } } |
Following are the benefits of Data Modeling
- It is very fast.
- A smart way to manipulation of data
- No extra layer of logic
- Really flexible to be modeled per need
- Setters can explicitly define what data can be loaded into the object
Q 3:- What is a class?
Answer- A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describes the details of an object
Q 4:- What is an object?
Answer- Object is termed as an instance of a class, and it has its own state, behavior, and identity.
Q 5:- What is Encapsulation?
Answer- Encapsulation is an attribute of an object, and it contains all data which is hidden. That hidden data can be restricted to the members of that class.
Levels are Public, Protected, Private, Internal and Protected Internal.
6. What is Polymorphism?
Answer- Polymorphism is nothing but assigning behavior or value in a subclass to something that was already declared in the main class. Simply, polymorphism takes more than one form.
Q 7:- What is Inheritance?
Answer- Inheritance is a concept where one class shares the structure and behavior defined in another class. Inheritance applied on one class is called Single Inheritance, and if it depends on multiple classes, then it is called multiple Inheritance
Q 8:- What is a difference between class and interface?
Answer-
- Interfaces do not contain business logic
- You must extend the interface to use.
- You can’t create an object of interface.
Q 9:- How to load classes in PHP?
Answer- We can load a class with the use of “autoload” class.
If we want to change from default function autoload to test upload function.
we can do this with “spl_autoload_register
1 |
spl_autoload_register('webpreparations'); |
Q 10:- What is the relation between Classes and Objects?
Answer- They look very much same but are not same. A class is a definition, while an object is an instance of the class created. A class is a blueprint while objects are actual objects existing in the real world. An example we have class CAR which has attributes and methods for Speed, Brakes, Type of Car etc.Class CAR is just a prototype, now we can create real-time objects which can be used to provide functionality. An example we can create a Maruti car object with 100 km speed and urgent brakes.
Q 11:- What is the difference between new and override?
Answer- The new modifier instructs the compiler to use the new implementation instead of the base class function. Whereas, Override modifier helps to override the base class function.
Q 12:- What are the various types of constructors?
Answer- There are three various types of constructors, and they are as follows:.
– Default Constructor – With no parameters.
– Parametric Constructor – With Parameters. Create a new instance of a class and also passing arguments simultaneously.
– Copy Constructor – Which creates a new object as a copy of an existing object.
Q 13:- What is early and late binding?
Answer- Early binding refers to the assignment of values to variables during design time whereas late binding refers to the assignment of values to variables during runtime.
Q 14:- What is ‘this’ pointer?
Answer- THIS pointer refers to the current object of a class. This keyword is used as a pointer which differentiates between the current object with the global object. Basically, it refers to the current object.
Q 15:- What are the difference between structure and a class?
Answer- Structure default access type is public, but class access type is private. A structure is used for grouping data whereas a class can be used for grouping data and methods. Structures are exclusively used for data and it doesn’t require strict validation, but classes are used to encapsulates and inherent data which requires strict validation.
Q 16:- What is the default access modifier in a class?
Answer- The default access modifier of a class is Private by default
How to call parent constructor?
1 |
parent::__construct() |
Q 17:- What is a pure virtual function?
Answer- A pure virtual function is a function which can be overridden in the derived class but cannot be defined. A virtual function can be declared as Pure by using the operator =0.
Example -.
1 2 3 |
Virtual void webpreparations() // Virtual, Not pure Virtual void webpreparationsdemo() = 0 //Pure virtual |
Q 18:- What are different visibility of method/property?
Answer- There are 3 types of visibility of method & property and are following
Public: Can be accessed from the same class method, child class and from outside of class.
Protected: Can be accessed from the same class method, child class.
Private: Can be accessed from same class method only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class WebpreparationsClass { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private'; function showValue() { echo $this->public; echo $this->protected; echo $this->private; } } $obj = new WebpreparationsClass(); echo $obj->public; // Works echo $obj->protected; // Fatal error: Cannot access protected property TestClass::$protected in echo $obj->private; // Fatal error: Cannot access private property TestClass::$private in C:\xampp\htdoc\webpreparations\class\class.php on line 20 $obj->showValue(); // Shows Public, Protected and Private |
Q 19:- What are base class, subclass and superclass?
Answer- Base class is the most generalized class, and it is said to be a root class.
The subclass is a class that inherits from one or more base classes.
Superclass is the parent class from which another class inherits.
Q 20:- What is Static Keyword in PHP?
Answer-
- If we declare a Method or Class Property as static, then we can access that without the use of instantiation of the class.
- Static Method is faster than Normal method.
- $this is not available within Static Method.
- Static properties cannot be accessed through the object(i.e arrow operator)
- Calling non-static methods with Scope Resolution operator generates an E_STRICT level warning.
- Static properties may only be initialized using a literal or constant value.
- Static properties/Normal properties Can’t be initialized using expressions value.
1 2 3 4 5 6 7 8 9 |
class WebStaticClass { public static $staticValue = 'foo'; public function staticValue() { return self::$my_static; } } echo WebStaticClass::$staticValue; |
Answer- Inheritance is the OOPS concept that can be used as reuse mechanism.
Q 22:- What are Properties of Object-Oriented Systems?
Answer-
- Inheritance
- Encapsulation of data
- Extensibility of existing data types and classes
- Support for complex data types
- Aggregation
- Association
Q 23:- What is Abstraction in PHP?
Answer-
- Abstraction is defined using the keyword abstract.
- PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated (create object).
- To extend the Abstract class extends operator is used.
- You can inherit only one abstract class at one time extending.
- Any class that contains one abstract method must also be declared as abstract. Methods defined as abstract simply declare the method’s signature, they can’t define the implementation.
- All methods marked as abstract in the parent’s class, the declaration must be defined by the child.
- additionally, these methods must be defined with the same (or a less restricted) visibility (Public, Protected & private).
- Type hint & number of the parameter must be matched between parent & child class.
Q 24:- What is Interface in PHP?
Answer-
- Interfaces are defined using the interface keyword.
- All methods declared in an interface must be public. Classes defined as Interface may not be instantiated(create object).
- To extend the interface class, implements operator is used.
- You can inherit a number of interface class at the time of extending and number of abstract class separated by a comma.
- All methods in the interface must be implemented within a child class; failure to do so will result in a fatal error.
- Interfaces can be extended like classes using the extends operator.
- The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error.
- Type hint & number of the parameter must be matched.
Q 25:- What is Overloading?
Answer- It dynamically creates method/properties and performed by magic methods. Overloading method/properties are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope, Means we you are calling a function which does not exist. None of the arguments of these magic methods can be passed by reference.
Q 26:- What is Final Keyword in PHP?
Answer- PHP introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final.
If the class itself is being defined final then it cannot be extended. If the function itself is being defined final then it cannot be extended
Q 27:- What is UML?
Answer- UML stands for Unified Modeling Language.
You can do following things with UML
- Manage project complexity.
- create the database schema.
- Produce reports