Yogesh Chauhan's Blog

PHP __construct() function

in PHP on June 9, 2021

Just like JavaScript constructor, PHP constructor initializes object’s properties when you create an object.

To add a constructor, you need to add a __construct() function in the class. So, when you create an object using that class, PHP will automatically call the __construct() function.

One of the advantage of adding a constructor is that you don’t need to add set_name() method and call it.

Syntax


__construct (optional values)


You can pass multiple values with different types of variables, though it’s optional. You can skip passing any values. It doesn’t have any return values.

PHP __construct() function example


<?php
class Car {
  public $make;
  public $model;

  function __construct($make,$model) {
    $this->make = $make; 
    $this->model = $model; 
  }
  function get_name() {
    return $this->make;
  }
  function get_model() {
    return $this->model;
  }
}

$car1 = new Car("Honda","Accord");
echo "The model for " . $car1->get_name() . " is " . $car1->get_model();
//The model for Honda is Accord
?>



What happens when there’s a child class?

When you define a child class constructor, the parent constructors are NOT called implicitly.

If you want to call the parent class constructor then use this inside child constuctor:


...
parent::__construct()
...


If the child class doesn’t have a constructor defined then it may be inherited from the parent class.


PHP __construct() function example with inheritance

Here’s a really good example from PHP official docs to understand the constructor with inheritance.


<?php
class BaseClass {
    function __construct() {
        print "In BaseClass constructor\n\n";
    }
}

class SubClass extends BaseClass {
    function __construct() {
        parent::__construct();
        print "In SubClass constructor\n\n";
    }
}

class OtherSubClass extends BaseClass {
    // inherits BaseClass's constructor
}

// In BaseClass constructor
$obj = new BaseClass();

// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();

// In BaseClass constructor
$obj = new OtherSubClass();
?>



Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Control Scrolling with CSS Scroll SnapCSSHigher Order Functions in JavaScript with ExamplesJavaScriptFile System Integrity: How to Keep an Eye on Your Files and Folder Change?MiscellaneousHow to add and remove list items using JavaScript?JavaScriptHow to apply style to the first-child using jQuery?jQueryHow to replace HTML lists using CSS Counters?CSSWhat’s a Strict mode in JavaScript?JavaScriptDebugging in WordPress Part 1: WP_DEBUGWordPressHow to convert a number rounding to a specified number of decimals in JavaScript?JavaScriptHow to get category name using post id in WordPress?WordPressHow to convert an HTML radio buttons into a toggle switch using CSS?CSSHow to uninstall Cocoapods from the Mac OS?Miscellaneous