1.2.671. Memoize MagicCall

Cache calls to magic methods in local variable. Local cache is faster than calling again the magic method as soon as the second call, provided that the value hasn’t changed.

__get is slower, as it turns a simple member access into a full method call. The caching is not possible if the processing of the object changes the value of the property.

<?php

class x {
    private $values = array();

    function __get($name) {
        return $this->values[$name];
    }
    // more code to set values to this class
}

function foo(x $b) {
    $a = $b->a;
    $c = $b->c;

    $d = $c;     // using local cache, no new access to $b->__get($name)
    $e = $b->a;  // Second access to $b->a, through __get
}

function bar(x $b) {
    $a = $b->a;
    $c = $b->c;

    $b->bar2(); // this changes $b->a and $b->c, but we don't see it

    $d = $b->c;
    $e = $b->a;  // Second access to $b->a, through __get
}

?>

Name

Default

Type

Description

minMagicCallsToGet

2

integer

Minimal number of calls of a magic property to make it worth locally caching.

See also __get performance questions with PHP, Make Magic Concrete and Benchmarking magic.

1.2.671.1. Suggestions

  • Cache the value in a local variable, and reuse that variable

  • Make the property concrete in the class, so as to avoid __get() altogether

1.2.671.2. Specs

Short name

Performances/MemoizeMagicCall

Rulesets

All, Analyze, Changed Behavior, Class Review

Exakat since

1.8.3

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

memoization

Available in

Entreprise Edition, Exakat Cloud