1. What keyword is used to declare a class in PHP?
Answer:
Explanation:
The 'class' keyword is used to declare a class in PHP.
2. How do you create an instance of a class in PHP?
Answer:
Explanation:
An instance of a class is created using the 'new' keyword followed by the class name.
3. What is the method inside a class that is called automatically when an object is created?
Answer:
Explanation:
The __construct() method is a constructor method in PHP that is called automatically when an object is created.
4. What is the visibility keyword that allows properties or methods to be accessible from anywhere?
Answer:
Explanation:
The 'public' visibility keyword allows properties or methods to be accessible from anywhere.
5. What keyword is used to define a constant inside a class in PHP?
Answer:
Explanation:
The 'const' keyword is used to define a constant inside a class in PHP.
6. What is it called when a class inherits the properties and methods of another class in PHP?
Answer:
Explanation:
Inheritance in PHP is achieved when a class 'extends' another class, inheriting its properties and methods.
7. What is encapsulation in PHP OOP?
Answer:
Explanation:
Encapsulation involves bundling the data (properties) and methods that operate on the data into a single unit or class.
8. How do you refer to the current object's properties or methods from within a class's methods in PHP?
Answer:
Explanation:
The $this keyword is used to refer to the current object's properties or methods within a class.
9. What is the meaning of polymorphism in PHP OOP?
Answer:
Explanation:
Polymorphism allows objects of different classes to be treated as objects of a common super class.
10. Which keyword is used to define a method that cannot be overridden in a child class in PHP?
Answer:
Explanation:
The 'final' keyword is used to define a method that cannot be overridden in a child class.
11. How do you create an abstract class in PHP?
Answer:
Explanation:
An abstract class is created using the 'abstract' keyword before the 'class' keyword.
12. What is an interface in PHP?
Answer:
Explanation:
An interface is a template that defines a set of methods that a class must implement.
13. What is the purpose of the 'static' keyword for properties and methods in PHP?
Answer:
Explanation:
Static properties and methods can be accessed directly without creating an instance of the class.
14. What is method overloading in PHP?
Answer:
Explanation:
Method overloading involves creating multiple methods with the same name but different parameters, which is not natively supported in PHP.
15. How is a property or method of a parent class referred to within a child class in PHP?
Answer:
Explanation:
The 'parent::' syntax is used to refer to a property or method of a parent class within a child class.
16. What is an object in the context of PHP OOP?
Answer:
Explanation:
In object-oriented programming, an object is an instance of a class.
17. What is the correct way to declare a property in a PHP class?
Answer:
Explanation:
A property in a PHP class is declared with a visibility keyword (like public) followed by the property name.
18. Can a PHP class have more than one constructor method?
Answer:
Explanation:
A PHP class can only have one constructor method, named __construct.
19. Which visibility keyword allows access to a property or method only within the class itself and its child classes?
Answer:
Explanation:
The 'protected' visibility keyword allows access within the class itself and its child classes.
20. What is a trait in PHP?
Answer:
Explanation:
Traits are a mechanism in PHP for reusing code in single inheritance languages, allowing a developer to reuse sets of methods freely in several independent classes.