Object Oriented Programming in PHP Flashcards
Q: What is Object-Oriented Programming (OOP)?
A: A programming paradigm based on the concept of objects, which encapsulate data and behaviour.
Q: What are the four main principles of OOP?
A: Encapsulation, Inheritance, Polymorphism, and Abstraction.
Q: How do you define a class in PHP?
class ClassName {
// Class properties and methods
}
Q: How do you create an object in PHP?
$object = new ClassName();
Q: What is a property in a PHP class?
A: A variable defined within a class to hold data.
Q: What are the three access modifiers in PHP?
A: public, protected, and private.
Q: What does the public access modifier mean?
A: The property or method is accessible from anywhere.
Q: What does the protected access modifier mean?
A: The property or method is accessible within the class and its subclasses.
Q: What does the private access modifier mean?
A: The property or method is accessible only within the class.
Q: How do you define a private property in PHP?
private $propertyName;
Q: What is a method in a PHP class?
A: A function defined within a class to perform actions.
Q: How do you define a method in PHP?
public function methodName() {
// Code
}
Q: How do you call a method of a class?
$object->methodName();
Q: What is the $this keyword in PHP?
A: A reference to the current object instance.
Q: Can methods have arguments in PHP?
A: Yes, just like regular functions.
Q: What is a constructor in PHP?
A: A special method automatically called when an object is instantiated.