Jump to content

uninitialized _construct()


rubing

Recommended Posts

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()?

Link to comment
https://forums.phpfreaks.com/topic/95072-uninitialized-_construct/
Share on other sites

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"

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 -  - "

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

Archived

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

×
×
  • 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.