Writing the constructor ijn PHP

Author Topic: Writing the constructor ijn PHP  (Read 1373 times)

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 158
  • Karma: 0
    • We Helpline
Writing the constructor ijn PHP
« on: 12-28-2015, 00:24:57 »
The constructor is a special method that is executed when an object is created using the class as a pattern. A constructor is not required, and you don’t need to use a constructor if you don’t want to set any property values or perform any actions when the object is created. Only one constructor is allowed.

The constructor has a special name so that PHP knows to execute the method when an object is created. Constructors are named __construct.(Note the two underscores.) A constructor method looks similar to the
following ...

function __construct()
$this->gas = 10; # starts with a full gas tank

This constructor defines the new car. When the car is created, it has a full gas tank and an open door.

Prior to PHP 5, constructors had the same name as the class. You may run across classes written in this older style. PHP 5 looks first for a method called __construct() to use as the constructor. If it doesn’t find one, it looks for a method that has the same name as the class and uses that method for the constructor. Thus, older classes still run under PHP 5.


Related Topics

  Subject / Started by Replies Last post
9 Replies
Last post 05-11-2020, 06:54:50
by mandiradebnath
1 Replies
Last post 10-11-2013, 00:56:25
by emedianetwork
2 Replies
Last post 12-17-2016, 17:42:20
by luffy268
0 Replies
Last post 09-07-2016, 05:27:55
by beingchinmay
5 Replies
Last post 07-08-2021, 10:01:58
by Lishmalinyjames