Home arrow PHP arrow Page 4 - Verifying Float Values with the Strategy Design Pattern

Using the FloatValidator class as a standalone component - PHP

In this third part of a series on validating incoming data with the strategy design pattern, I create a brand new strategy class that can check whether or not a supplied input value is a float number. The addition of this class extends the capabilities of the sample validation program that Iím building in this series.

  1. Verifying Float Values with the Strategy Design Pattern
  2. Review: the sample classes developed so far
  3. Adding a strategy class for checking float numbers
  4. Using the FloatValidator class as a standalone component
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
April 21, 2010

print this article



To demonstrate in a quick and dirty way how flexible the earlier ďFloatValidatorĒ class can be, below I coded a somewhat trivial script which shows how to use the class as a standalone component. Check it out:

// create an instance of the float validator class

$floatValidator = new FloatValidator('This is a test string');

// validate the supplied value

if (!$floatValidator->validate())


    echo $floatValidator->getFormattedError();


    displays the following

    The value This is a test string is incorrect. Please enter a float value.





    echo ' The data that you entered is correct.';


Done. As you can see, using an instance of the previous ďFloatValidatorĒ class in an independent way is a straightforward process that doesnít bear any further discussion. However, itís worth pointing out that the above script may produce  erroneous results, particularly when checking numeric values. These glitches occur due to the behavior of the FILTER_VALIDATE_FLOAT constant. Thus, if you need to fix these little bugs, please read the official PHP filters documentation here: http://php.net/manual/en/ref.filter.php

Having clarified that, Iím sure that at this point you have a clearer idea of how useful the Strategy pattern can be when checking incoming data in a modular way. However, the sample validation program being developed here still has a pretty limited functionality that needs to be enhanced. This will be done, of course, in future articles in this series.        

Final thoughts

In this third installment of the series, I proceeded to create a brand new strategy class, which was provided with the functionality required to check whether or not a supplied input value is a float number. While the addition of this class does contribute to extending the capabilities of the sample validation program that Iím currently building in this series, itís fair to say that it can be enhanced even further.

You may be wondering how. Well, itíd be relatively easy to add another strategy class to the program, and make this one able to validate email addresses. This would make it possible to assemble a wider range of validation strategies at run time, which speaks for itself about the benefits introduced by Composition. In the next article Iím going to define this new email checking class.

Hereís my final piece of advice: donít miss the upcoming tutorial!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: