PHP Classes

Checker: Validate the values of function arguments

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Info   View files Documentation   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2014-09-06 (1 year ago) RSS 2.0 feedNot yet rated by the usersTotal: 127 All time: 7,976 This week: 1,004Up
Version License PHP version Categories  
checker 1.0BSD License5.3PHP 5, Validation
Description Author  

This class can validate the values of function arguments.

It can take the name and the value of arguments passed to a function and check the values to verify if they are of the expected types.

Currently it can check if the argument value is a resource, callable, boolean, array, non-empty array, string, string or null, non-empty string, non-empty string or null, non-negative integer, non-negative integer or null, positive integer, positive integer or null, float, positive float, object, object or null.

When the argument does not match the expected type, the class throws an exception.

Picture of wapmorgan
  Performance   Level  
Name: wapmorgan is available for providing paid consulting. Contact wapmorgan .
Classes: 22 packages by
Country: Russian Federation Russian Federation
Age: ???
All time rank: 112936 in Russian Federation Russian Federation
Week rank: 194 Up9 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 8x

Winner: 1x


Checker is a utility for classes self-testing. It provides a useful set of testing methods that can test ins and outs for every method.

The idea

You declare a function or method like the following. It simply create a file with given filepath, fills it up with numberOfRetries, and handle some options via additionalParameters. `php function doSomeWork($filepath, $numberOfRetries, array $additionalParameters) { // .. $fmode = (isset($additionalParameters['fmode']) ? $additionalParameters['fmode'] : 'w'; $fp = fopen($filepath, $fmode); $numberOfRetries++; $i = 0; while ($numberOfRetries--) { fwrite($fp, "Try #".($i++)."\n"); } fclose($fp); return true; } ` This method is simply written, without any checking for errors in ins. It's possible that user's of your class code in purely innocence replace the first parameter with bool, isn't it? Your code in this situation will work unpredictable for you and most likely throws a php error in line $fp = fopen($filepath). The users of your code can use your classes wrong and most likely they do not want to harm.

To improve code quality and improve methods self-documenting in php I suggest using Checker. Previously listed function can use Checker and avoid occasional terminations of your code.


Checker has two implementations. Priority belongs to CheckerTrait, a trait. See example in examples/intro.trait.php.

For old code (5.3.x) and non-class functions exists Checker, a class. See example in examples/intro.class.php.

They both exists in SelfTestingCode namespace.

Work flow

Both realisations has the same methods:

  • checkResult(value, checker, options)
  • checkArgument([argName, argValue], checker, options)
  • check(value, checker, options)

You should not even try to call them from user-land code.


If you would like to add a checker, you need to create a trait (or class) extending CheckerTrait or Checker.

See CheckerTrait extending example in examples/extending.trait.php.

See Checker extending example in examples/extending.class.php.

Checker - is a simple static method in CheckerTrait or Checker that actually does call to checkResult() or checkArgument().


In place where you want test ins or outs of your program make a call to one of them (Checkers). Checkers testing ins has the arg prefix and has that signature: argScalar($argName, $argValue)

Checkers testings outs has the ret prefix and has that signature: retResource($value)

Testing effect

Checkers testing ins:

  • does not return any value
  • does not impact on arg value
  • uses argName in error output

See example in examples/testing.ins.php.

Checkers testing outs:

  • return the value
  • does not impact on value
  • uses 'return of result statement'

See example in examples/testing.outs.php.

Throwing errors

If any test fails, checker throws an exception with properly formed message explaining what type it was waiting for and what it got.


A lot of testing algorithms bundled into Checker.

  • ins tests * scalar * string * string-or-null * string-non-empty * string-non-empty-or-null * int * int-or-null * float * float-or-null * object-or-null array-of-instances.Accept 'class' parameter*.
  • outs tests * resource * callable * bool * array * empty-array * non-empty-array * int-positive * int-negative * float-positive * float-negative * int-positive-or-zero * int-negative-or-zero * float-positive-or-zero * float-negative-or-zero * object instance-of.Accept 'class' parameter.*

Checker method naming convention

in string-or-null = argStringOrNull

in string-non-empty-or-null = argNonEmptyStringOrNull

The same is applicable to out.

Outs testing

In order to provide transparent outs testing these checkers return value. See example in examples/testing.outs.php.

  Files folder image Files  
File Role Description
Files folder imageexamples (6 files)
Files folder imageSelfTestingCode (2 files)
Plain text file Checker.php Class Class file
Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file Doc. Documentation
Plain text file example.php Example Example script
Plain text file OldChecker.php Class Class source
Plain text file Data Auxiliary data
Plain text file Data Auxiliary data

  Files folder image Files  /  examples  
File Role Description
  Plain text file extending.class.php Example Example script
  Plain text file extending.trait.php Example Example script
  Plain text file intro.class.php Example Example script
  Plain text file intro.trait.php Example Example script
  Plain text file testing.ins.php Example Example script
  Plain text file testing.outs.php Example Example script

  Files folder image Files  /  SelfTestingCode  
File Role Description
  Plain text file Checker.php Class Class source
  Plain text file CheckerTrait.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 86%Total:127All time:7,976
 This week:0This week:1,004Up