Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    London, UK
    Thanked 110 Times in 109 Posts

    Newb PHP syntax question x2

    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:

    PHP 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:

    PHP Code:
    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??

  2. #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Bradford, UK
    Thanked 68 Times in 67 Posts
    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

  3. #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Denver, Colorado USA
    Thanked 510 Times in 498 Posts
    Edit: Too late again, we have to stop meeting this way... my wife is getting suspisious...

    All of your deductions are correct.

    PHP Code:
    $theSentry = new Sentry(); 
    Creates $theSentry, an "instance" of the class Sentry.
    PHP Code:
    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.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.


Posting Permissions

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