Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Constructors and Object Initialization | OOP Foundations in PHP
Object-Oriented PHP

bookConstructors and Object Initialization

The Idea: Initializing Objects with Constructors

When you create an object, you often want to set its initial state. Constructors are special methods that let you do this automatically when the object is created.

Code Example: Using a Constructor to Set Properties

Below, we define a Car class with a constructor that sets the color and brand when a new object is created.

CarExample.php

CarExample.php

copy
1234567891011121314
<?php class Car { public $color; public $brand; public function __construct($color, $brand) { $this->color = $color; $this->brand = $brand; } } // Creating a new Car object $myCar = new Car("red", "Toyota"); echo "My car is a " . $myCar->color . " " . $myCar->brand . ".";

In this example, the focus is on using a constructor. The __construct() method is a special function that automatically runs when a new object is created. It takes parameters $color and $brand and assigns them to the object’s properties using $this->color and $this->brand.

When we create a new Car object with new Car("red", "Toyota"), the constructor sets the properties immediately. Then we can access these properties with $myCar->color and $myCar->brand and use them to display a message about the car.

question mark

Which statement about constructors in PHP is correct?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain how constructors work in other programming languages?

What happens if I don't define a constructor in my class?

Can you show more examples of using constructors with different parameters?

bookConstructors and Object Initialization

Desliza para mostrar el menú

The Idea: Initializing Objects with Constructors

When you create an object, you often want to set its initial state. Constructors are special methods that let you do this automatically when the object is created.

Code Example: Using a Constructor to Set Properties

Below, we define a Car class with a constructor that sets the color and brand when a new object is created.

CarExample.php

CarExample.php

copy
1234567891011121314
<?php class Car { public $color; public $brand; public function __construct($color, $brand) { $this->color = $color; $this->brand = $brand; } } // Creating a new Car object $myCar = new Car("red", "Toyota"); echo "My car is a " . $myCar->color . " " . $myCar->brand . ".";

In this example, the focus is on using a constructor. The __construct() method is a special function that automatically runs when a new object is created. It takes parameters $color and $brand and assigns them to the object’s properties using $this->color and $this->brand.

When we create a new Car object with new Car("red", "Toyota"), the constructor sets the properties immediately. Then we can access these properties with $myCar->color and $myCar->brand and use them to display a message about the car.

question mark

Which statement about constructors in PHP is correct?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4
some-alt