B) Constructor
All objects can have a special built-in method called a ‘constructor’. Constructors allow you to initialise your object’s properties (translation: give your properties values,) when you instantiate (create) an object.
Note: If you create a __construct() function (it is your choice,) PHP will automatically call the __construct() method/function when you create an object from your class.
The ‘construct’ method starts with two underscores (__) and the word ‘construct’.
<?php class person { var $name; function __construct($persons_name) { $this->name = $persons_name; } function set_name($new_name) { $this->name = $new_name; } function get_name() { return $this->name; } } ?> |
If you want to explore more, visit our PHP edu & tutorials section!
Below are some examples:
In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…
We’re thrilled to announce an exciting opportunity for you to win not one but two…
Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…
Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…
Another week to bring you the top yield platforms for three of the most prominent…
If you hold a large volume of LEDU tokens above 1 million units and wish…