Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookProperties and Methods

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3
some-alt