php - Is it possible to pass an unset variable to a function or otherwise validate forms without isset()? -
i've got project passes json between frontend , backend. so, frontend php generate statement { "command" : "getuser", "parameters" : { "userid" : 1 } }
, send backend. backend executes
if ($command == 'getuser') { validate($parameters['userid']); if ($this->valid) { <<get user>> } }
validate checks variable , sets $this->valid
. creates error message if necessary. want create error message in case front end passes in { "command" : "getuser", "parameters" : "" }
. in case, $parameters['userid']
never set, , validate() complain passed unset variable.
i've come 2 solutions. first solution set $parameters['userid'] = "unset"
before loading in json, , check in validator. other solution use
if (isset($parameters['userid'])) { validate($parameters['userid']); } else { echo("error"); }
it'd nice if validate()
figure out on own whether or not variable there. there more elegant way should doing things?
in short, no. error happens as, rather after, validate() called there no way intercept that.
it sounds better solution have validateuser() function passed in whole $parameters variable check you're looking against 'user_id'. allows to further separate concerns (validation, versus response).
Comments
Post a Comment