OOPs PHP Questions & Answer
Object-oriented programming is an approach to programming where objects and classes are used. Object-Oriented PHP helps your code stay flexible by allowing it to be only defined once but used in many places. The goals of object-oriented programming are Increased understanding, Ease of maintenance. Ease of evolution.
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
The model view controller pattern is the most used pattern for today’s world web applications. At present there are more than a dozen PHP web frameworks based on MVC pattern.The MVC pattern separates an application in 3 modules:
Model, View and Controller:
Model - The model is responsible to manage the data; it stores and retrieves entities used by an application, usually from a database, and contains the logic implemented by the application.
View - The view (presentation) is responsible to display the data provided by the model in a specific format. It has a similar usage with the template modules present in some popular web applications, like wordpress, joomla, …
Controller - The controller handles the model and view layers to work together. The controller receives a request from the client, invokes the model to perform the requested operations and sends the data to the View. The view formats the data to be presented to the user, in a web application as an html output.
Polymorphism is the process of using an operator or function in different ways for different data input. In practical terms, polymorphism means that if class B inherits from class A, it doesn’t have to inherit everything about class A; it can do some of the things that class A does differently.
In general, polymorphism is the ability to appear in different forms. Technically, it is the ability to redefine methods for derived classes. Polymorphism is concerned with the application of specific implementations to an interface or a more generic base class.
The Scope Resolution Operator is a token that allows access to static, constant, and overridden properties or methods of a class. When referencing these items from outside the class definition, use the name of the class.
- Interfaces do not contain business logic.
- You must extend interface to use.
- You can't create object of interface.
We can load a class with the use of "autoload" class.
If we want to change from default function autoload to testautload function.
we can do this with "spl_autoload_register"
spl_autoload_register('class_name');
If constructor declared as private, PHP through the following fatal error when create object. Fatal error: Call to private BaseClass::__construct() from invalid context in. If constructor declared as protected, PHP through the following fatal error when create object. Fatal error: Call to protected BaseClass::__construct() from invalid context in.
- If we declare a Method or Class Property as static, then we can access that without use of instantiation of the class.
- Static Method are 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.
- Abstraction are 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 declare 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, 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 parameter must be match between parent & child class.
Hot Questions
How to Get the Size of all Tables in a database in MySQL?