How to use a function with an optional argument defined as the third

I use the elses class, and this person defined a function with five arguments.

in Sentry.php:

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

If all five fields are completed, this will lead to the login procedure.

Now on the page where he explains how to use this, there is a fragment that, according to php.net, does not make sense.

on the page that loads the sentinel:

require_once('../system/Sentry.php');
$theSentry = new Sentry();
if(!$theSentry->checkLogin(2)){ header("Location: login.php"); die(); }

which should behave by default so that it checks to see if the $ group argument is <= 10 (the default). In this situation, it should be two. If the user has a group variable <= 2 set, this should allow the person to view the page.

However, this does not work for a very obvious reason either: the php manual states:

, , ; .

, , phpbuilder.com, ($variable = default_something) , .

​​?:

checkLogin(2)
+3
5

- PHP- . Java :

public void login(String username)
public void login(String username, String password)

PHP :

function login($username, $password = '')

$username , $password . , . , . 1 , , "", .

, :

function checkLoginGroup($group = 10) {
  $this->checkLogin('', '', $group);
}

, , , :

$theSentry->checkLoginGroup(2);

( , ) "convience", .

, , , .

+9

- ... " " " "? ?
, , , ?

, , , , , ... ( , , - ) :

checkLogin('', '', 20);

-, :

checkLogin(null, null, 20);

: -, , , .

NULL, , , , , NULL ( , afterall!); - , ... ^^


,

  • ( , )

, IDE , ; :-(
phpdoc...

+1

Mythicas , : .

function checkLogin($args) {
    $defaults = array('user' => null, 'pass' => null, 'group' => null, ...);
    $args = array_merge($defaults, $args);
    ...
}

checkLogin(array('user' => 'x', 'group' => 9));

/ PHP, . , . , , PHP, .

+1

- , $.

0
checkLogin(NULL,NULL,2);

.

-1

Source: https://habr.com/ru/post/1714564/


All Articles