programing

정적 메서드가 아닌 메서드를 정적으로 호출하면 안 됩니다.

newnotes 2023. 9. 12. 20:41
반응형

정적 메서드가 아닌 메서드를 정적으로 호출하면 안 됩니다.

최근 PHP 5.4 업데이트를 했는데 정적 코드와 비적정 코드에 대한 오류가 발생했습니다.

오류입니다.

PHP Strict Standards:  Non-static method VTimer::get() 
should not be called statically in /home/jaco/public_html/include/function_smarty.php on line 371

371호선입니다.

$timer  = VTimer::get($options['magic']);

누가 도와줬으면 좋겠어요.

이는 다음과 같이 불러야 한다는 뜻입니다.

$timer = (new VTimer)->get($options['magic']);

사이의 차이static그리고.non-static첫 번째 것은 인스턴스화가 필요하지 않기 때문에 전화를 걸 수 있습니다.classname그다음에 덧붙이기::즉시 메소드를 호출합니다.이와 같습니다.

ClassName::method();

메서드가 정적이 아니면 다음과 같이 초기화해야 합니다.

$var = new ClassName();
$var->method();

그러나 PHP >=5.4에서는 이 구문을 축약어로 대신 사용할 수 있습니다.

(new ClassName)->method();

다음과 같이 메서드를 정적으로 변경할 수도 있습니다.

class Handler {
    public static function helloWorld() {
        echo "Hello world!";
    }
}

가장 우아한 방법은 다음과 같습니다.

(new ClassName)->method();

함수를 다음으로 변환할 수도 있습니다.static function call() {}, 하지만 그것은 당신의 기능과 당신이 그것을 가지고 무엇을 하느냐에 달려있습니다.

클래스를 인스턴스화해야 하는 경우 클래스를 인스턴스화하지 않고 정적 함수를 상수와 같이 취급하면 개체를 가질 수 없으며 미리 정의된 변수가 필요합니다.

public function functionName($variable)

다음으로 변경

public static function functionName($variable)

언급URL : https://stackoverflow.com/questions/19693946/non-static-method-should-not-be-called-statically

반응형