SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
Thread: constructors?
-
Dec 17, 2006, 18:58 #1
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;
}
-
Dec 17, 2006, 19:22 #2
- 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.
-
Dec 17, 2006, 19:26 #3
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()
-
Dec 17, 2006, 19:28 #4
- 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";
-
Dec 17, 2006, 19:34 #5
-
Dec 17, 2006, 19:37 #6
- 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.
-
Dec 17, 2006, 19:40 #7
- 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
-
Dec 17, 2006, 19:44 #8
-
Dec 17, 2006, 22:04 #9
- 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