1.2.886. Optional Parameter

An optional parameter is a method argument that has both a typehint and a default value.

Such argument is optional, as it may be omitted. When this is the case, the code has to differentiate between the default behavior or the actual usage. It is recommended to avoid providing a default value, and use a null object.

<?php

class foo {
    function methodWithOptionalArgument(bar $x = null) {
        if ($x === null) {
            // default behavior
        } else {
            // normal behavior
        }
    }

    function methodWithCompulsoryArgument(bar $x) {
        // normal behavior
        // $x is always a bar.
    }
}
?>

1.2.886.1. Specs

Short name

Functions/OptionalParameter

Rulesets

All

Exakat since

0.12.4

PHP Version

All

Severity

Time To Fix

Precision

Very high

Features

parameter, optional-parameter

Available in

Entreprise Edition, Exakat Cloud