Using $ this when not in an object context a call method inside a class

I have this line of code that calls buildField method from Field class name

$field_dictionary[$key] = Field::buildField($key, $request);

      

The Field class uses the buildField method here

public function buildField($key, $request) {

    $field_vocabulary = [];

    $image = $_FILES[$key];
    $image['tmp_name']['image'] = true;
    // Calling this another method from same class
    $field = $this->sanitizeFieldRows($image['tmp_name'], $request->post($key . '_name'), $request->post($key . '_description'));

    $field_vocabulary['name'] = implode('|', $field->field_1);
    $field_vocabulary['description'] = implode('|', $field->field_2);
    $field_vocabulary['image'] = implode('|', $field->reference);

    return $field_vocabulary;
}

      

This code has a line like this

$field = $this->sanitizeFieldRows($image['tmp_name'], $request->post($key . '_name'), $request->post($key . '_description'));

      

I am calling another method from the same class. It does some function that I just removed for so long.

public function sanitizeFieldRows($reference, $field_1, $field_2 = null) {

    // Some code etc.....
    // Outputs an object
    return (object) $output;
}

      

But the point is what I call $this->sanitizeFieldRows($par1,$par2,$par3)

, but it throws an error:

Using $this when not in object context in

      

But when I did Field::sanitizeFieldRows($par1,$par2,$par3)

, it works, but this method is in the same object, but it is not a static method that I am calling.

Is there something wrong with that?

Here are the same questions:

Using $ this if not in the context of an object?

Using $ this if not in an object context

Using $ this if not in an object context

PHP using $ this if not in object context

Fatal error: using $ this if not in object context

Fatal error: Using $ this if not in the context of an explain context?

Using $ this if not in php object context

+3


source to share


1 answer


Since buildField is a static method, this $ variable is not available in its scope.

Because static methods are called without an instantiated object, the $ this pseudo-variable is not available inside a static method.



http://php.net/manual/en/language.oop5.static.php

+2


source







All Articles