200+ PHP Interview Questions Flashcards
Q17. What is the difference between multiple and multilevel inheritance?
Multiple inheritance means a class has more than one parent. It can use methods and properties from all parents, but sometimes there are conflicts (diamond problem).
Multilevel inheritance is a chain. One class inherits from another, then another. It gets all properties from previous classes, and there is no diamond problem.
Q17.a. What are traits in PHP?
Traits are a way to reuse code in different classes. They are like small pieces of a class that you can “copy” into many classes.
Traits are not classes - they cannot be used alone, only inside a class.
Q17.b. Why use traits?
To share methods between different classes.
To avoid duplicate code.
Q17.c. Problems with traits in PHP
- Method conflicts - if two traits have the same method name, you must choose which one to use.
use TraitA, TraitB { TraitA::method insteadof TraitB; }
- No real inheritance - You cannot override them, and they do not support polymorphism
- Hard to read code - uf class uses many traits, it is difficult to understand where methods come from.
- Overuse - Some developers use traits instead of inheritance or composition, making code messy
Q17.d. Best use of traits?
Traits are good for reusing code (like logging or caching), but they do not replace inheritance and composition.
Q18. What is a Closure in PHP and why do we use it?
Closure is the class, which represents anonymous functions. This means that you can store the function to any variable and this variable will be the object of the Closure class.
- Can be passed as arguments to other functions
- Can access variables from outside their scope
- Useful for short, reusable logic (sorting, filtering, callbacks)
Closures are common in event handling, function programming and asynchronous tasks
Q19.a. What is session and how it works?
Session is a way to store user data on the server side
Q19.b. What is trigger?
Triggers are used to execute a set of SQL statements when a specific event occurs.
- To automate actions (logging, validation)
- To update related tables (update stock after a sale)
Triggers are useful, but too many triggers can slow down performance
Q19.c. What is View?
Views are virtual tables that display data from one or more tables
Q19.d. What is GET and POST?
GET and POST are HTTP methods used to send data to a server.
GET:
- Data is sent in the URL
- Often used for retrieving data (displaying a webpage)
- Limit on the amount of data you can send
- Not secure for sensitive data (like password)
POST:
- Data is sent in the body of the request
- Used for sending large amounts of data
- More secure then GET for sensitive data
- No data sie limit like GET
In summary, GET is for getting data, and POST is for sending data