i have a validation class which needs improving. If I require some custom validation I need to specify a custom function. It works a bit like this:
- The controller tells the validation that a custom validation function is required.
- The controller runs the validation.
- — Gets iffy here —
- Validation class creates a new instance of the controller class….
- Validation class runs controller->custom_validation_function()
- Validation class returns true / false
Is there someway that I can alter this to do something like this?
$validation = new validation;
// Insert rules here.
$validation->function() = $this->function();
By doing this I could get rid of the step of creating an unneeded class instance.
You will want to use a Strategy Pattern. Have a Validator class for each validation you want to do and then instantiate this class in the consuming class as needed. Then delegate the validation to the validator instance.
Have a look at how Zend Framework does it for an idea.
Also see the example given in my answer to this related question: