rubing Posted March 8, 2008 Share Posted March 8, 2008 I am reading an outstanding php book by Marc Wandschneider called 'Core Web Application Development with PHP and MySQL'. However, he does something funny with the _construct() function when delving into the creation of a UserManager class. Here is his script: <?php //Database, username, password require_once('dbconn.inc'); require_once('errors.inc'); class UserManager { function _construct() { //we don't have initialization just yet } } Now, I thought the purpose of _construct() was stipulate parameters that must be assigned to the object at time of initialization. What does leaving it blank do? How is this different than not having a _construct()? Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 8, 2008 Share Posted March 8, 2008 try naming the constructor function with 2 underscores alternatively name the function exactly the same as the class name Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 8, 2008 Share Posted March 8, 2008 oops did not read your post. leavingit blank will do nothing, the function will initialize but no code is in the function so it wont do anything. constructors are used to initialize code at the time the class is created eg: <?php //Database, username, password require_once('dbconn.inc'); require_once('errors.inc'); class UserManager { function _construct($string) { echo("Class initialized - $string"); } } $usermanagerclass = &New UserManager("hello"); ?> would ouput "Class Initialized - Hello" Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 8, 2008 Share Posted March 8, 2008 "What does leaving it blank do? How is this different than not having a _construct()?" as far as i understand, leaving it blank doesn't do anything and it is no different than not having a _construct(). Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 8, 2008 Share Posted March 8, 2008 also, youcan initialize class variables like so: <?php Class classname { var $name="mr_blobby"; var $password=null; var $current_user; function __construct(){ echo($this->name." - ".$this->password." - ".$this->current_user); } } $newclass = &New classname(); ?> would ouput: "mr_blobby - - " Quote Link to comment Share on other sites More sharing options...
rubing Posted March 8, 2008 Author Share Posted March 8, 2008 That's really cool, but why are you placing '&' in front of keyword 'New' &New ??? Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 9, 2008 Share Posted March 9, 2008 http://forums.whirlpool.net.au/forum-replies-archive.cfm/833742.html Quote Link to comment Share on other sites More sharing options...
corbin Posted March 9, 2008 Share Posted March 9, 2008 So what's the point of creating it with the &? Why not just pass it with the &? function test(&$class) {} Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 9, 2008 Share Posted March 9, 2008 the & in front of new has no purpose as i can tell, i just use it as it was used by a book i once read. i have tried some examples and the & does not seem to affect anything i can see. more information about classes can be found here: http://uk.php.net/manual/en/language.oop5.basic.php so yeah, new will work the same as &new, ill keep using the & just incase Quote Link to comment Share on other sites More sharing options...
rubing Posted March 9, 2008 Author Share Posted March 9, 2008 I don't know brotha, using that ampersand would make me feel deprecated. I just want to feel 'New' ...<insert bad joke drum sounds> 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.