Jump to content

Class constructor


Guest askjames01

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?


Link to comment
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.


Link to comment
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).
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.