View Full Version : Newb PHP syntax question x2

04-04-2007, 04:58 PM
I've inherited a half-started intranet. Great. It's in PHP and I'm an ASP programmer. If I have to remember to stop putting "then" on the end of IF statements once more, I'm going to hit something.

Anyway. Two questions, from the following code:

$theSentry = new Sentry();
if (!$theSentry->checkLogin(2) ){ header("Location: index.php"); die(); }

There are two things here I can't fathom. One is the -> notation. I can't find a reference to it on www.php.net, although I daresay there is if you're not an idiot. I'm assuming that it simply means "call a function of the parent class", similar to the "." in JS or VBscript?

The second is the function itself. It's defined in Sentry.php as:

function checkLogin($user = '',$pass = '',$group = 10,$goodRedirect = '',$badRedirect = ''){

Which... I'm guessing... means a function with 5 arguments, all of which have default values if they're not sent in the function call. But I still don't get how checkLogin(2) would work. Is that what it's doing? Just calling the function with one argument? How??

04-04-2007, 05:03 PM
yes the -> is calling a function of the object' class (it can also be used to access object variables)

Since all the arguments for the function have default values, all of them are optional when calling the function. calling the function as checkLogin(2) is saying call the function with the user of 2. If the call was checkLogin(2, 'Bob') it would be user =2 and password - 'bob'. Variables are passed in the order they are specified in the declaration from left to right and since they're all optional you can stop at any point, however if you wanted to specify $group, you would also have to specify user and password - hope that makes sense

04-04-2007, 05:13 PM
Edit: Too late again, we have to stop meeting this way... my wife is getting suspisious...

All of your deductions are correct.

$theSentry = new Sentry();Creates $theSentry, an "instance" of the class Sentry.

$theSentry->checkLogin(2)This calls the checkLogin "member" function of that class.

The value of 2, as the only parameter, will be assigned to the $user variable in the function code (replacement of parameters is left to right as you would expect.) I am guessing that this value triggers some special processing code in the function. Normally you would pass user supplied input in the form of variables in the function call for the $user and $pass parameters.