Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Properties and Methods | OOP Foundations in PHP
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Object-Oriented PHP

bookProperties and Methods

The Idea: Giving Objects Data and Behavior

To make your objects more useful, you add properties (data) and methods (behavior) to your classes. For example, a Car might have a color and a brand as its properties, and it might be able to drive, which is a method. This approach allows you to model real-world things and what they can do directly in your code.

Code Example: Adding Properties and Methods to a Class

Below, you will see how to define a Car class with properties and a method.

car.php

car.php

copy
1234567891011121314151617
<?php class Car { public $color; public $brand; public function drive() { echo "The $this->color $this->brand is driving."; } } // Example usage: $myCar = new Car(); $myCar->color = "red"; $myCar->brand = "Toyota"; $myCar->drive(); // Output: The red Toyota is driving.

The keyword class is used to declare a new class named Car. Inside it, two properties are defined with public $color; and public $brand;. The public keyword means these properties can be accessed from outside the class. The drive() method is declared with public function drive(), and inside it $this->color and $this->brand refer to the current object’s properties.

Below the class, a new object is created using new Car(). After that, its properties are assigned with the syntax $myCar->color = "red";. The arrow operator -> is used both to set properties and to call methods. Finally, calling $myCar->drive() executes the method defined inside the class.

question mark

Which of the following statements about the Car class are true?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

bookProperties and Methods

Glissez pour afficher le menu

The Idea: Giving Objects Data and Behavior

To make your objects more useful, you add properties (data) and methods (behavior) to your classes. For example, a Car might have a color and a brand as its properties, and it might be able to drive, which is a method. This approach allows you to model real-world things and what they can do directly in your code.

Code Example: Adding Properties and Methods to a Class

Below, you will see how to define a Car class with properties and a method.

car.php

car.php

copy
1234567891011121314151617
<?php class Car { public $color; public $brand; public function drive() { echo "The $this->color $this->brand is driving."; } } // Example usage: $myCar = new Car(); $myCar->color = "red"; $myCar->brand = "Toyota"; $myCar->drive(); // Output: The red Toyota is driving.

The keyword class is used to declare a new class named Car. Inside it, two properties are defined with public $color; and public $brand;. The public keyword means these properties can be accessed from outside the class. The drive() method is declared with public function drive(), and inside it $this->color and $this->brand refer to the current object’s properties.

Below the class, a new object is created using new Car(). After that, its properties are assigned with the syntax $myCar->color = "red";. The arrow operator -> is used both to set properties and to call methods. Finally, calling $myCar->drive() executes the method defined inside the class.

question mark

Which of the following statements about the Car class are true?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 3
some-alt