arrow_back Back to Curriculum
code

PHP (Core & OOP)

Fundamental PHP structures, Object-Oriented patterns, traits, and interface implementations highly expected by top software houses in Pakistan.

Interview Questions

25 Total
Q1.

What is the main difference between an Interface and an Abstract Class in PHP?

Medium

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.

Q2.

What are Traits in PHP and how do they solve the multiple inheritance problem?

Medium

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.

Q3.

Explain the concept of Late Static Binding in PHP.

Hard

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.

Q4.

How would you prevent SQL Injection in vanilla PHP?

Easy

By using Prepared Statements (PDO or MySQLi), filtering inputs, and escaping outputs, rather than directly concatenating variables into SQL strings.

Q5.

What is the difference between the '==' and '===' operators in PHP?

Easy

The '==' operator checks for value equality, while the '===' operator checks for both value and type equality.

Q6.

How do you implement inheritance in PHP?

Easy

Inheritance in PHP is implemented using the 'extends' keyword, which allows a child class to inherit properties and methods from a parent class.

Q7.

What is the purpose of the 'final' keyword in PHP?

Medium

The 'final' keyword is used to prevent a method from being overridden in a child class or to prevent a class from being inherited.

Q8.

What is the difference between 'public', 'private', and 'protected' access modifiers in PHP?

Easy

'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.

Q9.

How do you implement polymorphism in PHP?

Medium

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.

Q10.

What is the purpose of the '__construct' method in PHP?

Easy

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.

Q11.

How do you handle errors and exceptions in PHP?

Medium

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.

Q12.

What is the difference between 'include' and 'require' statements in PHP?

Easy

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.

Q13.

How do you implement the Singleton design pattern in PHP?

Hard

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.

Q14.

What is the purpose of the 'autoload' function in PHP?

Medium

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.

Q15.

How do you use PHP's built-in 'ReflectionClass' to get information about a class?

Hard

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.

Q16.

What is the difference between 'foreach' and 'while' loops in PHP?

Easy

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.

Q17.

How do you use PHP's built-in 'Iterator' interface to create custom iterators?

Hard

PHP's 'Iterator' interface can be used to create custom iterators by implementing its methods, such as 'rewind', 'current', 'key', 'next', and 'valid'.

Q18.

What is the purpose of the 'namespace' keyword in PHP?

Medium

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.

Q19.

How do you use PHP's built-in 'Closure' to create anonymous functions?

Medium

PHP's 'Closure' can be used to create anonymous functions by assigning a function to a variable without declaring a named function.

Q20.

What is the difference between 'serialize' and 'json_encode' in PHP?

Easy

The 'serialize' function converts a PHP object into a serialized string, while the 'json_encode' function converts a PHP object into a JSON string.

Q21.

How do you use PHP's built-in 'SplFileObject' to read and write files?

Hard

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'.

Q22.

What is the purpose of the 'magic methods' in PHP, such as '__get' and '__set'?

Medium

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.

Q23.

How do you use PHP's built-in 'ArrayObject' to create objects that behave like arrays?

Hard

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'.

Q24.

What is the difference between 'abstract' and 'interface' in PHP?

Medium

An 'abstract' class can contain both abstract and concrete methods, while an 'interface' can only contain abstract methods.

Q25.

How do you use PHP's built-in 'DateTime' class to work with dates and times?

Easy

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'.