Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Properties and Methods | OOP Foundations in PHP
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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain what the `$this` keyword does in the method?

What does the `public` keyword mean for properties and methods?

Can you show how to create multiple `Car` objects with different properties?

bookProperties and Methods

Scorri per mostrare il 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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 3
some-alt