PHP Tutorial

A web server programming language.

PHP Constructor

PHP Constructor

A constructor allows you to initialize an object's properties upon creation of the object.

If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

Why use a constructor?

It saves us from calling the set_name() method which reduces the amount of code.

Try It Yourself

Edit the code below and click Run to see the result live.

Source Code
Result
Topics