Jump to content

Prevent class method to be executed if parameters missing


flaab

Recommended Posts

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.

Link to comment
Share on other sites

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;
       }

}

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.