Jump to content

Class constructor

  • Please log in to reply
2 replies to this topic

#1 Guest_askjames01_*

  • Guests

Posted 03 May 2006 - 04:44 AM

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]

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. . . */

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?

#2 Guest_askjames01_*

  • Guests

Posted 03 May 2006 - 06:49 AM

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.

#3 wildteen88

  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 May 2006 - 11:00 AM

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).

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users