1.2.1401. Useless Typehint

__get() and __set() magic methods won’t enforce any typehint. The name of the magic property is always cast to string.

__call()

<?php

class x {
    // typehint is set and ignored
    function __set(float $name, string $value) {
        $this->$name = $value;
    }

    // typehint is set and ignored
    function __get(integer $name) {
        $this->$name = $value;
    }

    // typehint is checked by PHP 8.0 linting
    // typehint is enforced by PHP 7.x
    function __call(integer $name) {
        $this->$name = $value;
    }
}

$o = new x;
$b = array();
// Property will be called 'Array'
$o->{$b} = 2;

// type of $m is check at calling time. It must be string.
$o->{$m}();

?>

See also __set.

1.2.1401.1. Suggestions

  • Use string for the $name parameter

  • Use no typehint for the $name parameter

1.2.1401.2. Specs

Short name

Classes/UselessTypehint

Rulesets

All, Changed Behavior, Class Review, Suggestions

Exakat since

2.1.9

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

magic-method

Available in

Entreprise Edition, Exakat Cloud