Writing the constructor ijn PHP

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

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 139
  • Karma: 0
    • No Addiction
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
6 Replies
Last post 04-23-2012, 23:48:10
by jerryvannorman
1 Replies
Last post 10-11-2013, 00:56:25
by emedianetwork
5 Replies
Last post 12-12-2014, 02:41:21
by Stephen
0 Replies
Last post 03-14-2016, 19:50:46
by hap53144
0 Replies
Last post 09-07-2016, 05:27:55
by beingchinmay