Properties 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
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.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
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?
Fantastiskt!
Completion betyg förbättrat till 6.67
Properties and Methods
Svep för att visa menyn
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
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.
Tack för dina kommentarer!