Guest askjames01 Posted May 3, 2006 Share Posted May 3, 2006 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? Quote Link to comment Share on other sites More sharing options...
Guest askjames01 Posted May 3, 2006 Share Posted May 3, 2006 ok, thanks for no replies.i re-read the manual andi 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. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 3, 2006 Share Posted May 3, 2006 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). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.