Question

I have to pass a parameter to a function.

public function accept($user){

if(! $user){

 //Show not Found page.

 }else{

  //do something else

}

If the parameter is empty the NOT Found page is shown but also php errors for missing argument.

Severity: Warning

Message: Missing argument 1 for Controller::accept()

Filename: controllers/controller.php

What should i do that this errors are not shown? Shall i use php "Getters" and "Setters"? Is there a convinient way to check whether parameters are passed for several functions (instead of writing every time in the beginning of a method "if(isset($param))"?

Était-ce utile?

La solution

instead of this:

public function accept($user){

Do this:

public function accept($user = false){

By using $user = false You don't need to add a variable to the function because if it not exists it will be false.

So now you can use these two methods to acces the function

accept();
//$user will be false in the function

and

$idUser = 1;
accept($idUser);
//$user will be 1 in the function

Also see for more information about isset(), empty() and ! this url: http://kunststube.net/isset/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top