Member Function Flashcards

1
Q

What is a member function in PHP?

A

A member function is a function defined within a class that operates on the properties of that class.

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

Provide an example of a class with a member function in PHP.

A

class Car {
public $color; // Property of the class
public function construct($color) { // Constructor to initialize the color
$this->color = $color; // Assign the color value to the property
}
public function displayColor() { // Member function to display the color
echo “The color of the car is: “ . $this->color; // Access the property using $this
} }

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

How do you create an object of a class in PHP?

A

$myCar = new Car(“red”); // Create an instance of the Car class with color ‘red’

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

How do you call a member function on an object in PHP?

A

$myCar->displayColor(); // Call the displayColor member function on the $myCar object
// Outputs: The color of the car is: red

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

What are the key points about member functions in PHP?

A

Member functions are defined within a class.
They can access class properties using $this.
They are called on objects created from the class.
They encapsulate behavior related to the class’s data.

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