Probability distribution superclass
If you examine the JSci Probability Distribution superclass (
ProbabilityDistribution.java), you would notice obvious similarities to the PHP version of this class shown in Listing 3. One major difference, however, is that the PHP version is designed so that it can co-exist nicely with other PEAR classes.
PEAR is short for PHP Extension and Application Repository and is the official structured library of open source code for PHP users. The PEAR Group also advocates a standard style for code written in PHP. The recommended PEAR coding style and good Java programming style have many similarities. These similarities mean that it is relatively easy to turn good Java code into PEAR-conformant code.
Three main issues arise and can cause some difficulties in porting code from Java to PHP4:
- Lack of native support for namespaces in PHP means that my class names are longer than you might see in Java (such as
- Lack of native support for polymorphous constructors in PHP
means that rather than declaring your class with a variable number of
arguments which cause different constructors to be invoked, you try to
achieve the same effect through setting argument defaults and doing
different things depending on whether a default argument is supplied or
what type of argument it is. Often I cannot achieve the same effect in
PHP (except through ugly workarounds), so I might just implement the
most commonly used constructor.
Also, in PHP you do not need to statically define the type of your function arguments. When calling any PHP function for any given argument slot, you can pass in a single value or an array as your argument. Within such PHP functions, you can add logic that detects the argument type and uses this information to carry out different operations. In other words, you can use PHP's type-indifferent argument-passing protocol and type-detection code to achieve constructor polymorphism.
- Lack of native support for advanced exception handling in PHP (which will change with PHP5) means that you have to rely upon the forward-compatible
PEAR.phperror handling class to flag and deal with errors.
While support for these OO features in PHP would be desirable, they are arguably not necessary; workarounds can achieve similar effects. The tradeoff is that PHP easier to learn and to use productively for solving Web scripting problems.
The probability distribution superclass (Listing 3) defines the methods that need to be instantiated by all probability distribution classes. It also defines methods and constants that can be used by child classes.Listing 3. Probability distribution superclass, PHPMath_ProbabilityDistribution_General
It should be noted that I modified the JSci API to use common
textbook abbreviations for accessing the core distribution functions
I also enforce the idea that all classes should instantiate a Random
Number Generating (RNG) method. RNG methods in particular are generally
not as easy to implement as the other methods and may be one reason
they were not included in the initial implementation of the Probability
I have also provisionally added a
PHPMATH_MAX_FLOAT constant and added a
utility method. A more mature PHP Math library might include these
generally useful constants and methods in separate files so that they
could be included in a wider range of math classes.