Writing the constructor ijn PHP

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

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 158
  • Karma: 0
    • Hot Shaper
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 ...

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

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
2398 Views
Last post 04-23-2012, 23:48:10
by jerryvannorman
1 Replies
1830 Views
Last post 10-11-2013, 00:56:25
by emedianetwork
2 Replies
1979 Views
Last post 12-17-2016, 17:42:20
by luffy268
0 Replies
583 Views
Last post 09-07-2016, 05:27:55
by beingchinmay
0 Replies
511 Views
Last post 03-06-2017, 05:09:09
by beingchinmay