# Method Call on Non-Object

> Section in “New and Extended Functionality › Error Handling” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/error-handling/method-call-on-non-object/>

PHP strictly distinguishes between objects and scalar values. While an object can contain methods, in PHP a scalar type like a string or a boolean is not represented as an object and thus does not provide any methods that can be called.

Consequently, executing the following code leads to an error:

``` php
$x = 'not-an-object';
$x->method();
```

Even before attempting to call the requested method, PHP refuses to continue because of the unsupported type:

    PHP Fatal error:  Call to a member function method() on string in ...

This is a very common mistake, seen mostly when an explicit type declaration is missing on a method’s signature or when the returned value’s type was not verified. This error also got replaced by an engine exception in PHP 7 and thus can now be handled with a simple `catch`:

``` php
$x = 'not-an-object';
try {
    $x->method();
} catch (Error $error) {
    var_dump($error);
}
```

Executing the code shown will render the following output

    object(Error)#1 (7) {
      ["message":protected]=>
      string(44) "Call to a member function method() on string"
      ...

---

Previous: [Recoverable Fatal Errors](https://php7explained.com/new-and-extended-functionality/error-handling/recoverable-fatal-errors/index.html.md) | Next: [CompileError](https://php7explained.com/new-and-extended-functionality/error-handling/compileerror/index.html.md)
