flaab Posted September 7, 2007 Share Posted September 7, 2007 Hi =) You see i want to prevent a class method to be executed if any parameters are missing Example code: class foo{ public function whatever($param1, $param2, $param3) { // Whatever logic } } Well, if I execute it without any required parameter...it displays a warning message in the browser and get executed badly! I want the method NOT to be executed if some parameter is missing. How can i do that? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/68385-prevent-class-method-to-be-executed-if-parameters-missing/ Share on other sites More sharing options...
micah1701 Posted September 7, 2007 Share Posted September 7, 2007 can you just add something like if(!isset($param1) || !isset($param2) || !isset($param3)){ return false; } Quote Link to comment https://forums.phpfreaks.com/topic/68385-prevent-class-method-to-be-executed-if-parameters-missing/#findComment-343813 Share on other sites More sharing options...
lemmin Posted September 7, 2007 Share Posted September 7, 2007 That will still give the missing param warning. Try this: class foo{ public function whatever($param1=null, $param2=null, $param3=null) { if ($param1==null || $param2==null || $param3==null) return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/68385-prevent-class-method-to-be-executed-if-parameters-missing/#findComment-343816 Share on other sites More sharing options...
micah1701 Posted September 7, 2007 Share Posted September 7, 2007 That will still give the missing param warning. Try this: class foo{ public function whatever($param1=null, $param2=null, $param3=null) { if ($param1==null || $param2==null || $param3==null) return false; } } good call! Quote Link to comment https://forums.phpfreaks.com/topic/68385-prevent-class-method-to-be-executed-if-parameters-missing/#findComment-343823 Share on other sites More sharing options...
flaab Posted September 7, 2007 Author Share Posted September 7, 2007 Thabks for your answers! But I need that to be done from outside of the method code, not from inside. The params are given by an url mapper. I need to convert that warning into an execution error. Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/68385-prevent-class-method-to-be-executed-if-parameters-missing/#findComment-343842 Share on other sites More sharing options...
micah1701 Posted September 7, 2007 Share Posted September 7, 2007 then use the same code snippet provided, but check it before calling the function... Quote Link to comment https://forums.phpfreaks.com/topic/68385-prevent-class-method-to-be-executed-if-parameters-missing/#findComment-343843 Share on other sites More sharing options...
lemmin Posted September 7, 2007 Share Posted September 7, 2007 Why would you have to do it outside the function? If you change the "return false;" to "die("EXCEPTION ERROR");" you should get what you are looking for. If you don't have access to the function, why don't you just check that all the variables are set before executing the function, as micah1701 suggested? Quote Link to comment https://forums.phpfreaks.com/topic/68385-prevent-class-method-to-be-executed-if-parameters-missing/#findComment-343845 Share on other sites More sharing options...
flaab Posted September 7, 2007 Author Share Posted September 7, 2007 I need it to be from outside of the function code cause i'm programming a web developing framework that should not allow any action to be executed if some parameters are missing in the URL. http://www.whatever.com/foo/whatever/param1/param2 will execute the method "whatever" of the class "foo". But if the "whatever" method needs 3 parameters as those in the previos examples, the method "whatever" should not be executed and an error message should be displayed instead, because the result of the method execution would be corrupt. I do not know how many parameters are going to need the functions programmed by the framework's users, so I need a general solution. Is there a php function that returns how many parameters does a certain function require to be executed? Ideas? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/68385-prevent-class-method-to-be-executed-if-parameters-missing/#findComment-343855 Share on other sites More sharing options...
Jessica Posted September 7, 2007 Share Posted September 7, 2007 I think you might be going about this the wrong way. But anyway I don't think there is a function like what you're asking. You'd have to define somewhere how many arguments each function needs. Or edit the core PHP code, I guess? Quote Link to comment https://forums.phpfreaks.com/topic/68385-prevent-class-method-to-be-executed-if-parameters-missing/#findComment-343858 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.