Call to a member function %s() on %s

Description

This error happens when the object syntax is used on a string. A string is a scalar type, and cannot hold method, properties nor constants. The type of the variable should be checked before usage. This also happens with the nullsafe operator, which only protects against NULL used as an object.

Example

<?php

'a'?->foo();

$string = 'abc';
$string->foo();

?>

Literal Examples

  • Call to a member function foo() on string

  • Call to a member function foo() on int

  • Call to a member function foo() on null

Solutions

  • Check the type of the object before usage.