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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.