IT박스

PHP 치명적 오류 : 객체 컨텍스트에 없을 때 $ this 사용

itboxs 2020. 7. 11. 11:08
반응형

PHP 치명적 오류 : 객체 컨텍스트에 없을 때 $ this 사용


문제가 있습니다 :

프레임 워크없이 새로운 WebApp을 작성하고 있습니다.

index.php에서 나는 다음을 사용하고있다 :require_once('load.php');

그리고에 load.php 내가 사용하고 require_once('class.php');내로드 class.php을 .

class.php 에서이 오류가 발생했습니다 :

치명적 오류 : class.php의 객체 컨텍스트에 없을 때 $ this 사용 ... (이 예제에서는 11입니다)

class.php 를 작성 하는 방법의 예 :

class foobar {

    public $foo;

    public function __construct() {
        global $foo;

        $this->foo = $foo;
    }

    public function foobarfunc() {
        return $this->foo();
    }

    public function foo() {
        return $this->foo;
    }
}

index.php에서foobarfunc() 다음과 같이 로드 하고 있습니다.

foobar::foobarfunc();

하지만 또한

$foobar = new foobar;
$foobar->foobarfunc();

왜 오류가 발생합니까?


내 index.php에서 다음과 같이 foobarfunc ()을로드하고 있습니다.

 foobar::foobarfunc();  // Wrong, it is not static method

하지만 또한

$foobar = new foobar;  // correct
$foobar->foobarfunc();

정적 메소드가 아니기 때문에이 방법으로 메소드를 호출 할 수 없습니다.

foobar::foobarfunc();

대신 다음을 사용해야합니다.

foobar->foobarfunc();

그러나 정적 메소드를 작성한 경우 다음과 같습니다.

static $foo; // your top variable set as static

public static function foo() {
    return self::$foo;
}

그런 다음 이것을 사용할 수 있습니다 :

foobar::foobarfunc();

정적이 아닌 메소드를 호출하고 있습니다.

public function foobarfunc() {
    return $this->foo();
}

정적 호출 사용 :

foobar::foobarfunc();

정전기 전화를 사용하는 경우, 함수가 호출됩니다 (으로 선언되지 않더라도 static) 객체의 어떤 인스턴스가 없기 때문에,하지만, 전혀 없다 $this.

그래서 :

  • 비 정적 메소드에 정적 호출을 사용해서는 안됩니다.
  • 정적 메소드 (또는 정적으로 호출되는 메소드)는 정적 호출을 사용할 때 클래스 인스턴스가 없으므로 일반적으로 클래스의 현재 인스턴스를 가리키는 $ this를 사용할 수 없습니다.


여기서 클래스의 메서드는 클래스의 $foo속성에 액세스해야하므로 클래스의 현재 인스턴스를 사용하고 있습니다.

즉, 메소드에는 클래스의 인스턴스가 필요합니다. 즉 정적 일 수 없습니다.

This means you shouldn't use static calls : you should instanciate the class, and use the object to call the methods, like you did in your last portion of code :

$foobar = new foobar();
$foobar->foobarfunc();


For more informations, don't hesitate to read, in the PHP manual :


Also note that you probably don't need this line in your __construct method :

global $foo;

Using the global keyword will make the $foo variable, declared outside of all functions and classes, visibile from inside that method... And you probably don't have such a $foo variable.

To access the $foo class-property, you only need to use $this->foo, like you did.


If you are invoking foobarfunc with resolution scope operator (::), then you are calling it statically, e.g. on the class level instead of the instance level, thus you are using $this when not in object context. $this does not exist in class context.

If you enable E_STRICT, PHP will raise a Notice about this:

Strict Standards: 
Non-static method foobar::foobarfunc() should not be called statically

Do this instead

$fb = new foobar;
echo $fb->foobarfunc();

On a sidenote, I suggest not to use global inside your classes. If you need something from outside inside your class, pass it through the constructor. This is called Dependency Injection and it will make your code much more maintainable and less dependant on outside things.


First you understand one thing, $this inside a class denotes the current object.
That is which is you are created out side of the class to call class function or variable.

So when you are calling your class function like foobar::foobarfunc(), object is not created. But inside that function you written return $this->foo(). Now here $this is nothing. Thats why its saying Using $this when not in object context in class.php

Solutions:

  1. Create a object and call foobarfunc().

  2. Call foo() using class name inside the foobarfunc().


When you call the function in a static context, $this simply doesn't exist.

You would have to use this::xyz() instead.

To find out what context you're in when a function can be called both statically and in an object instance, a good approach is outlined in this question: How to tell whether I’m static or an object?


Fast method : (new foobar())->foobarfunc();

You need to load your class replace :

foobar::foobarfunc();

by :

(new foobar())->foobarfunc();

or :

$Foobar = new foobar();
$Foobar->foobarfunc();

Or make static function to use foobar::.

class foobar {
    //...

    static function foobarfunc() {
        return $this->foo();
    }
}

$foobar = new foobar; put the class foobar in $foobar, not the object. To get the object, you need to add parenthesis: $foobar = new foobar();

Your error is simply that you call a method on a class, so there is no $this since $this only exists in objects.


Just use the Class method using this foobar->foobarfunc();

참고URL : https://stackoverflow.com/questions/2350937/php-fatal-error-using-this-when-not-in-object-context

반응형