I am currently writing code and want to make sure all the params that get passed to a function/method are valid. Since I am writing in PHP I don't have access to all the facilities of other languages like C, C++ or Java to check for parameters values and types

public function inscriptionExists($sectionId, $userId) // PHP

vs.

public boolean inscriptionExists(int sectionId, int userId) // Java

So I have to rely on exceptions if I want to make sure that my params are both integers.

Since I have a lot of places where I need to check for param validity, what would be the best way to create a validation/exception machine and avoid code duplication?

I was thinking on a static factory (since I don't want to pass it to all of my classes) with a signature like:

You might be interested in looking at how QuickConnect structures things. It goes through a validation stage before the major computation and view related stuff. The php version has not received much love recently though github.com/yenrab/qcnative
–
JoshRagemDec 12 '12 at 15:06

In my opinion, dynamic typing is one of the features of PHP that makes it highly enjoyable to work with. Instead of enforcing strict typing rules, you can just document your function really well to indicate the parameters you expect. If the contract is not met, then results may vary and the fault is on the user of your function.

Plus if your code base is used only internally (by you and your team), you should have no problems with parameter types as you developped the code and should know how to use it. If your project is a library available to the public, good documentation is key. You should also have good unit tests to make sure your code behaves as expected and to provide examples on how to use your library.

I see you are trying to write Java code in PHP and that means that you still don't understand PHP well. When writing code in a given language, you should try to embrace the language features/philosophy instead of looking back to what you already know and trying to adapt.

I am not trying to write Java code in PHP, I used it only as an exemple, what I'm really trying to do is to make sure that all the pre condition for the function are met to make sure that I have a stable result and an higher traceability of the differents bugs that can occur du to bad method usage
–
JF DionDec 12 '12 at 15:39

@JFDion Well this is one of the risks of using a dynamic language. Fortunately, the advantages really outweight it and you should learn to take advantage of the dynamic nature of the language. I find that good documentation and unit tests really help to avoid such kind of bugs and bad usage.
–
marco-fisetDec 12 '12 at 15:46

The PHP code is making a shit toward this since you can have your function and force parameters to be arrays or instances of classes/interfaces, but since int, strings and other simple stuctures aren't managed through objects, you can't validate them based on type like in : function myFunction(array $myArray, myClass $object)
–
JF DionDec 12 '12 at 15:57

@JFDion While it is true that PHP provides such functionality, I recommend against using it. You don't seem to grok the advantages of duck typing and dynamic languages in general. PHP is a very badly designed language and is a trash full of unrelated partly-implemented features. Possibility to use static typing is one of those uncompleted features as you can't force primitive types as you mentioned. Once you go past these atrocities, PHP is a nice guy. You should try to avoid static typing altogether.
–
marco-fisetDec 12 '12 at 16:02