Jump to content

Archived

This topic is now archived and is closed to further replies.

Guest askjames01

Class constructor

Recommended Posts

Guest askjames01
ok, the manual said that a Class constructor purpose is this;

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below). [/quote]

example:
[code]class Cart {
   var $todays_date;
   var $name;
   var $owner;
   var $items = array("VCR", "TV");

   function Cart() {
       $this->todays_date = date("Y-m-d");
       $this->name = $GLOBALS['firstname'];
       /* etc. . . */
   }
}[/code]

so cart() is the constructor right?
so meaning constructor is "To initialize variables with non-constant values".

so my main question is (is this really the only purpose of constructor?)
or there is another purpose?


Share this post


Link to post
Share on other sites
Guest askjames01
ok, thanks for no replies.
i re-read the manual and
i found only this;

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Constructors are functions in a class that are automatically called when you create a new instance of a class with new. A function becomes a constructor, when it has the same name as the class. If a class has no constructor, the constructor of the base class is being called, if it exists. [/quote]

so i think there is no other purpose.


Share this post


Link to post
Share on other sites
All wat the constructor does is just setups the variables or calls other internal methods (functions) inside the class. There are two ways to define a constructor either create a function as the same name as the class name (the php4 way) or use the __construct function (PHP5 only).

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.