Method Not Property Name Must Be String¶
<?php
class X {
static public $p = 1;
static function foo() {}
function __toString() : string {
return 'foo';
}
}
$name = []; // try with different type : bool, int, float, object..
try {
echo X::$name;
} catch (Error $e) {
print $e->getMessage();
}
$name = [];
try {
echo X::${$name}();
} catch (Error $e) {
print $e->getMessage();
}
?>
A dynamic property name must be a string, and it must exist. It cannot be anything else, as not casting to string happens.
On the other hand, a dynamic property name is forcefully cast to a string before accessing its value.
See Also¶
PHP Features¶
Last updated: 14 July 2026