SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: constructors?

Hybrid View

  1. #1
    SitePoint Enthusiast fredanthony's Avatar
    Join Date
    Aug 2006
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    constructors?

    Can someone explain what the section of code defining the function __construct below does?
    PHP Code:
    class userAuth {
        
    // define properties
        
    private $username;
        private 
    $passwd;
        private 
    $passwdFile;

        
    // constructor
        // must be passed username and non-encrypted password
        
    public function __construct($username$password) {
                
    $this->username $username;
                
    $this->passwd $password;
        } 

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I may be wrong, but it just formats the variables $username and $passwd, so you can use them in the $this->username format and change them down in the script using "$this->username = 'value';". I'm pretty sure that __contstruct() is automatically called in the class and doesn't need any call from the script to do it's job.

  3. #3
    SitePoint Enthusiast fredanthony's Avatar
    Join Date
    Aug 2006
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but can't I just use $this->username without the constuct? Is it just initializing these variables? Thanks, I would just like to understand the purpose of __construct()

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Constructor as the name suggests is automatically called when an object is created from a class.
    In this case there are two parameters to the constructor ($username, $password) which means you would not be able to create a userAuth object without supplying these values.

    In this case the constructor is being used to ensure that those values are passed to the object. It then just assigns them as object members so that any other method (function) in the class can access them.

    If the constructor didn't have those parameters you could be trying to authenticate the user with no username or password.

    A simple use-case of this class could look something like this:
    PHP Code:
    $auth = new userAuth('admin''super_secret_password');
    if(
    $auth->isAdmin()) echo "Welcome admin";
    else echo 
    "Bugger off"

  5. #5
    SitePoint Enthusiast fredanthony's Avatar
    Join Date
    Aug 2006
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    Constructor as the name suggests is automatically called when an object is created from a class.
    In this case there are two parameters to the constructor ($username, $password) which means you would not be able to create a userAuth object without supplying these values.

    In this case the constructor is being used to ensure that those values are passed to the object. It then just assigns them as object members so that any other method (function) in the class can access them.

    If the constructor didn't have those parameters you could be trying to authenticate the user with no username or password.

    A simple use-case of this class could look something like this:
    PHP Code:
    $auth = new userAuth('admin''super_secret_password');
    if(
    $auth->isAdmin()) echo "Welcome admin";
    else echo 
    "Bugger off"
    so what would happen if the constructor wasn't there? Thanks.

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    then those variables wont be assigned a value.
    i think you can guess what that will result in.

  7. #7
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If there was no constructor there'd be nothing forcing you to supply a username and password to the object!
    By making these paramaters of the constructor you cannot create an auth object without them. As clamcrusher said -- there's not much point trying to authenticate without a username and password

  8. #8
    SitePoint Enthusiast fredanthony's Avatar
    Join Date
    Aug 2006
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    If there was no constructor there'd be nothing forcing you to supply a username and password to the object!
    By making these paramaters of the constructor you cannot create an auth object without them. As clamcrusher said -- there's not much point trying to authenticate without a username and password
    Ah, ok, I see. Thanks so much cranial-bore and clamcrusher!

  9. #9
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and for the record, you dont NEED the constructor. but in this example, you would need to set those variables in some other way. you could do this with another method, you will just need to manually call that method with the variables after instantiating the object.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •