Object Oriented Programming in PHP Flashcards

1
Q

Q: What is Object-Oriented Programming (OOP)?

A

A: A programming paradigm based on the concept of objects, which encapsulate data and behaviour.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: What are the four main principles of OOP?

A

A: Encapsulation, Inheritance, Polymorphism, and Abstraction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: How do you define a class in PHP?

A

class ClassName {
// Class properties and methods
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: How do you create an object in PHP?

A

$object = new ClassName();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: What is a property in a PHP class?

A

A: A variable defined within a class to hold data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: What are the three access modifiers in PHP?

A

A: public, protected, and private.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: What does the public access modifier mean?

A

A: The property or method is accessible from anywhere.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: What does the protected access modifier mean?

A

A: The property or method is accessible within the class and its subclasses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: What does the private access modifier mean?

A

A: The property or method is accessible only within the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: How do you define a private property in PHP?

A

private $propertyName;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: What is a method in a PHP class?

A

A: A function defined within a class to perform actions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: How do you define a method in PHP?

A

public function methodName() {
// Code
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: How do you call a method of a class?

A

$object->methodName();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q: What is the $this keyword in PHP?

A

A: A reference to the current object instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q: Can methods have arguments in PHP?

A

A: Yes, just like regular functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q: What is a constructor in PHP?

A

A: A special method automatically called when an object is instantiated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Q: What is the syntax for defining a constructor in PHP?

A

public function __construct() {
// Initialization code
}

18
Q

Q: What is a destructor in PHP?

A

A: A special method called when an object is destroyed or the script ends.

19
Q

Q: What is the syntax for defining a destructor in PHP?

A

public function __destruct() {
// Cleanup code
}

20
Q

Q: Can constructors have arguments in PHP?

A

A: Yes, arguments can be passed during object creation.

21
Q

Q: What is inheritance in OOP?

A

A: A mechanism where a class can inherit properties and methods from another class.

22
Q

Q: How do you extend a class in PHP?

A

class ChildClass extends ParentClass {
// Additional properties and methods
}

23
Q

Q: What is the parent keyword used for?

A

A: To call a parent class’s method or constructor.

24
Q

Q: What is method overriding in PHP?

A

A: Redefining a parent class’s method in a child class.

25
Q

Q: Can private properties be inherited?

A

A: No, private properties are not accessible in child classes.

26
Q

Q: What is polymorphism in OOP?

A

A: The ability to use the same method name with different implementations in derived classes.

27
Q

Q: How is polymorphism achieved in PHP?

A

A: Through method overriding.

28
Q

Q: What is the purpose of the final keyword in PHP?

A

A: To prevent a class from being extended or a method from being overridden.

29
Q

Q: What is an abstract class in PHP?

A

A: A class that cannot be instantiated and may contain abstract methods.

30
Q

Q: What is an abstract method in PHP?

A

A: A method declared in an abstract class but not implemented.

31
Q

Q: What is an interface in PHP?

A

A: A blueprint for classes that defines methods a class must implement.

32
Q

Q: How do you define an interface in PHP?

A

interface InterfaceName {
public function methodName();
}

33
Q

Q: How do you implement an interface in a class?

A

class ClassName implements InterfaceName {
public function methodName() {
// Implementation
}
}

34
Q

Q: Can a class implement multiple interfaces in PHP?

A

A: Yes, a class can implement multiple interfaces.

35
Q

Q: What is the difference between an abstract class and an interface?

A

A: An abstract class can have implemented methods, while an interface cannot.

36
Q

Q: What is the static keyword in PHP?

A

A: Used to define properties or methods that belong to the class, not instances.

37
Q

Q: How do you access a static method in PHP?

A

ClassName::staticMethod();

38
Q

Q: What is a trait in PHP?

A

A: A mechanism for reusing methods across multiple classes.

39
Q

Q: How do you include a trait in a class?

A

class ClassName {
use TraitName;
}

40
Q

Q: Can you use multiple traits in a class?

A

A: Yes, a class can use multiple traits.