1.2.891. 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 <https://www.php.net/`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.891.1. Connex PHP features

1.2.891.1.1. Specs

Short name

Functions/OptionalParameter

Rulesets

All, Changed Behavior

Exakat since

0.12.4

PHP Version

All

Severity

Time To Fix

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud