call a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

call a function

Post by ed209 »

sorry for the vague title, not really sure how to sum this problem up.

I am making a class that creates and validates forms. On creating an input field in the form, I want to specify what validation function should be used for that field.

The validation check will probably be done by a function. That function will be defined in the class. I can't post the code as there's too much so I'll try and give an example.

My Class:

Code: Select all

<?php
class myFormClass{

//...rest of the class up here 

function vaildateEmail($email){
      if( this is an $email ) ? return true : return false;
}

}
?>
my form page where I make the form - and where the data posts back to (action is itself):

Code: Select all

<?php
$my_form = new myFormClass;

$my_form->makeAnInputField(various parameters here, array("vaildateEmail", "maybe another parameter..."));

?>

I want my data checker function in 'myFormClass' (not shown) to use the "vaildateEmail" function to check the data for that field. The only way my data checker function will know which function to use is because of this first element of the passed array.

I have considered using 'case' i.e. if the first element of the array == "vaildateEmail" then call that function. The reason I don't want to do that is because I just want to add new functions into my class as I need them without having to alter it in lots of places like switch statements.

Am I making any sense???
Thanks for the help in advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

What you really want is a rule based validation. It is probably the best solution and a clean OO one (but not mentioned here much):

Code: Select all

<?php
$my_form = new myFormClass;

$my_form->addRule('fieldname', new EmailRule ("error message if rule fails"));
?>
Rules usually inherit a base rule class. They look like:

Code: Select all

<?php
class MyRule {
    function MyRule($errmsg) {}
    function isValid($value) {}
}
?>
(#10850)
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post by ed209 »

thanks for the help. I was looking for something like call_user_func(), I knew I had seen it somewhere but couldn't find it on php.net - didn't really know what to search for, I did stumble across is_callable() and get_defined_functions().

The rules look interesting though, I've never heard of them.

So would I create a validation class separate from my form making class and add functions to validate the submitted data, or would my form making functions and validation functions be better off in the same class?

I'm not really sure I understand how to implement it. In your example, is 'addRule' a function you have defined?
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post by ed209 »

I've used call_user_func() with some success but I have encountered a problem which I thought migh be relevant to this post, basically, it's a problem with setting a class wide variable from a function called by 'call_user_func()'.

The post can be seen here:


viewtopic.php?p=235882#235882
Post Reply