Hello and welcome to our community! Is this your first visit?
Register
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
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    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:
    require_once('inc/Sentry.php');
    $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
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 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
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,049
    Thanks
    2
    Thanked 317 Times in 309 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:
    $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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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