Call to a member function extension() on null

The error “call to a member function extension() on null” occurs when a method is called on a null object. This means that the variable which the method is being called on doesn’t have a valid object assigned to it.

To better understand this error, let’s consider an example:

    
      <?php
        $filename = null;
        $extension = $filename->extension(); // This will cause the error
      
        // Further code...
      ?>
    
  

In the given example, the variable $filename is set to null. Then, the method extension() is called on $filename. Since $filename is null, there is no object to call the extension() method on, resulting in the “call to a member function extension() on null” error.

To fix this error, you need to ensure that the variable has a valid object assigned to it before calling any methods on it. Here’s an updated example:

    
      <?php
        $filename = new SomeClass(); // Create an instance of SomeClass
        $extension = $filename->extension(); // Call extension() method
      
        // Further code...
      ?>
    
  

In this updated example, $filename is created as an instance of SomeClass, which has the extension() method. Now, when the extension() method is called on $filename, it will execute without any error.

Make sure to replace “SomeClass” in the example with the actual class or object you are working with.

Same cateogry post

Leave a comment