PHP

PHP Menu

PHP

Constructor and Destructor - PHP OOP

When creating a new object, you can pass a list of arguments to the class being called. These are passed to a special method within the class, called the constructor, which initializes various properties. This is done using the __construct() function.

The following is an example of a class with a constructor function. This is the same example as in the previous chapter, but the setting of the properties are initialized.

<?php
class People { // These are the class properties public $name; public $country;
// The constructor function function __construct($name, $country) { $this->name = $name; $this->country = $country; } // More class methods function get_name() { return $this->name; } function get_country() { return $this->country; } }
// new instance of the People() class $alex = new People('Alexander','UK'); // accessing the object properties echo "Name: " . $alex->get_name(); echo "<br>"; echo "Country: " . $alex->get_country();

PHP Destructors

PHP also has the ability to create destructor methods. This ability is useful when code has made the last reference to an object or when a script reaches the end. This is done by creating a __destruct() function. Let's take the example above and make a destructor function.

<?php
class People { // These are the class properties public $name; public $country;
// The constructor function function __construct($name, $country) { $this->name = $name; $this->country = $country; } function __destruct() { echo "Name: " . $this->name; echo "<br>"; echo "Country: " . $this->country; } }
// new instance of the People() class $alex = new People('Alexander','UK');

Exercise

Create a class named Car with name and year as its properties. Create the class constructor a destructor functions. The constructor sets the name and year, while the destructor prints these properties using the format : Name - Year. Create an instance of your class with the object name, $ford with its name as Ford and year as 2021.

<?php
<?php
class Car { public $name; public $year;
function __construct($name, $year) { $this->name = $name; $this->year = $year; } function __destruct() { echo $this->name . " - " . $this->year; } }
$ford = new Car('Ford', '2021');
{ "test_output_contains": { "expected":"Ford - 2021", "error_message":"Sorry, wrong output. Check your format." }, "test_variable_exists": [ { "object":"$ford", "error_message":"Have you declared <code>$ford<\/code>?" }, { "object":"$name", "error_message":"Have you declared <code>$name<\/code>?" }, { "object":"$year", "error_message":"Have you declared <code>$year<\/code>?" } ], "test_function_exists": [ { "object":"__construct", "error_message":"Did you create <code>__construct<\/code>?" }, { "object":"__destruct", "error_message":"Did you create <code>__destruct<\/code>?" } ], "success_message":"Good job!", "error_message":"There is something wrong on your code." }

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods