PHP (Core & OOP)
Fundamental PHP structures, Object-Oriented patterns, traits, and interface implementations highly expected by top software houses in Pakistan.
Interview Questions
25 TotalWhat is the main difference between an Interface and an Abstract Class in PHP?
An interface can only contain method signatures (no implementation), and a class can implement multiple interfaces. An abstract class can have both signatures and implementations, but a class can only inherit from one abstract class.
What are Traits in PHP and how do they solve the multiple inheritance problem?
Traits are a mechanism for code reuse. A trait is similar to a class, but it is purely intended to group functionality in a fine-grained and consistent way. They allow developers to reuse sets of methods freely in several independent classes, overcoming single inheritance limitations in PHP.
Explain the concept of Late Static Binding in PHP.
Late static binding is used to reference the called class in a context of static inheritance using the `static::` keyword, rather than `self::` which refers to the class where the method is defined.
How would you prevent SQL Injection in vanilla PHP?
By using Prepared Statements (PDO or MySQLi), filtering inputs, and escaping outputs, rather than directly concatenating variables into SQL strings.
What is the difference between the '==' and '===' operators in PHP?
The '==' operator checks for value equality, while the '===' operator checks for both value and type equality.
How do you implement inheritance in PHP?
Inheritance in PHP is implemented using the 'extends' keyword, which allows a child class to inherit properties and methods from a parent class.
What is the purpose of the 'final' keyword in PHP?
The 'final' keyword is used to prevent a method from being overridden in a child class or to prevent a class from being inherited.
What is the difference between 'public', 'private', and 'protected' access modifiers in PHP?
'public' members can be accessed from anywhere, 'private' members can only be accessed within the same class, and 'protected' members can be accessed within the same class and its child classes.
How do you implement polymorphism in PHP?
Polymorphism in PHP can be achieved through method overloading or method overriding, where multiple methods with the same name can be defined with different parameters or return types.
What is the purpose of the '__construct' method in PHP?
The '__construct' method is a special method in PHP that is automatically called when an object of a class is created, and is used to initialize the object's properties.
How do you handle errors and exceptions in PHP?
PHP provides a built-in exception handling mechanism using try-catch blocks, where the 'try' block contains the code that might throw an exception, and the 'catch' block contains the code that handles the exception.
What is the difference between 'include' and 'require' statements in PHP?
The 'include' statement includes a file and generates a warning if the file is not found, while the 'require' statement includes a file and generates a fatal error if the file is not found.
How do you implement the Singleton design pattern in PHP?
The Singleton design pattern in PHP can be implemented using a class that has a private constructor, a private static instance, and a public static method that returns the instance.
What is the purpose of the 'autoload' function in PHP?
The 'autoload' function in PHP is used to automatically include classes and interfaces when they are instantiated, without the need for explicit 'include' or 'require' statements.
How do you use PHP's built-in 'ReflectionClass' to get information about a class?
PHP's 'ReflectionClass' can be used to get information about a class, such as its properties, methods, and constants, by creating an instance of the class and using its methods.
What is the difference between 'foreach' and 'while' loops in PHP?
The 'foreach' loop is used to iterate over arrays, while the 'while' loop is used to iterate over a block of code as long as a certain condition is true.
How do you use PHP's built-in 'Iterator' interface to create custom iterators?
PHP's 'Iterator' interface can be used to create custom iterators by implementing its methods, such as 'rewind', 'current', 'key', 'next', and 'valid'.
What is the purpose of the 'namespace' keyword in PHP?
The 'namespace' keyword in PHP is used to group related classes, interfaces, and functions under a unique name, to avoid naming conflicts and improve code organization.
How do you use PHP's built-in 'Closure' to create anonymous functions?
PHP's 'Closure' can be used to create anonymous functions by assigning a function to a variable without declaring a named function.
What is the difference between 'serialize' and 'json_encode' in PHP?
The 'serialize' function converts a PHP object into a serialized string, while the 'json_encode' function converts a PHP object into a JSON string.
How do you use PHP's built-in 'SplFileObject' to read and write files?
PHP's 'SplFileObject' can be used to read and write files by creating an instance of the class and using its methods, such as 'read' and 'write'.
What is the purpose of the 'magic methods' in PHP, such as '__get' and '__set'?
The 'magic methods' in PHP, such as '__get' and '__set', are special methods that are automatically called when certain actions are performed on an object, such as getting or setting a property.
How do you use PHP's built-in 'ArrayObject' to create objects that behave like arrays?
PHP's 'ArrayObject' can be used to create objects that behave like arrays by creating an instance of the class and using its methods, such as 'offsetGet' and 'offsetSet'.
What is the difference between 'abstract' and 'interface' in PHP?
An 'abstract' class can contain both abstract and concrete methods, while an 'interface' can only contain abstract methods.
How do you use PHP's built-in 'DateTime' class to work with dates and times?
PHP's 'DateTime' class can be used to work with dates and times by creating an instance of the class and using its methods, such as 'format' and 'modify'.