1 <?php
2 class ACL
3 {
4 var $perms = array(); //Array : Stores the permissions for the user
5 var $userID = 0; //Integer : Stores the ID of the current user
6 var $userRoles = array(); //Array : Stores the roles of the current user
7
8 function __constructor($userID = '')
9 {
10 if ($userID != '')
11 {
12 $this->userID = floatval($userID);
13 } else {
14 $this->userID = floatval($_SESSION['userID']);
15 }
16 $this->userRoles = $this->getUserRoles('ids');
17 $this->buildACL();
18 }
This is part of a tutorial I’m learning creating a login system. I don’t know if I was at this for too long and my brain is fried but I’m getting confused about the boolean statement used as an argument in the function __constructor. See line 8.
This is the explanation given by the tutorial:
The __constructor() function is used to initialize the object when we
want to load an ACL. It is called automatically when we call new
ACL();. It is then passed a single, optional argument of the user to
load the ACL for. Inside the constructor, we check to see if a user ID
was passed in. If no ID was passed, we assume that we will load the
ACL for the currently logged in user; so we read in the session
variable for that. Alternatively, if we pass in a user ID, it allows
us to read and edit the ACL for a user other than the one logged in
(useful for your admin page).
Question #1… so this method is starting up as soon as you make a new class, and it is taking the boolean as an argument. I’m not understanding why you need to state the fact that userID = ‘ ‘. Couldn’t you just make it simpler and write: function __constructor($userID) { if ($userID != ”) ……..etc. etc. ?? Or do they mean the same thing?
The idea of stating a boolean never made sense to me, especially when you’re not using it in an IF statement to test it’s validity.
EDIT: I made a very weird slip. Sorry disregard the whole boolean thing, it’s not even a boolean statement. My error… 12 hours of learning programming is taking its toll.
Question 2: Is the variable $userID in line 5 the same as $userID in line8? I’m thinking no but I’m not sure. I’m sure that $userID on line 5 is the same as $this->userID on line 12 and line 15 but what about on line 8 and 10? I am confused. If the variable $userID on line 8 is NOT blank, then you are assigning $userID found on line 5 to the floatval of $userID on line 8??
I have a feeling that these are 2 different $userIDs. Why couldn’t this guy just use a different word if that’s the case? LOL
NOTE: I’ve been learning PHP for less than a week so these will seem like basic conceptual questions more than anything. Syntax isn’t really a problem for me, it’s the concepts.
Any help appreciated. Cheers.
Okay,
new ClassName()statement is called. The reasonfunction __construct($userID = '')is used and notfunction __construct($userID), is because we want to give it a default value. Meaning a user can either callnew APC()ornew APC("userID").The one on line 8 is a function argument. When the method is called with an argument (
new APC("userID")) that argument will be stored in that variable inside of the function.It’s coincidental that both of them have the same name.