라이브러리

[PHP] Deprecated::__construct - 새로운 Deprecated 속성 인스턴스를 구성합니다.




Deprecated::__construct


PHP 7.2 버전부터 `__construct` 메소드는 deprecated 상태로 변경되었습니다. 이는 PHP 8.0 버전부터 완전히 제거되었습니다.

# 왜 Deprecated 되었을까?


`__construct` 메소드는 PHP 5.3 버전부터 사용할 수 있게되었습니다. 이 메소드는 클래스의 생성자를 정의하는 데 사용되었습니다. 하지만, PHP 7.2 버전부터는 `__construct` 메소드를 사용하는 대신, `__invoke` 메소드를 사용하는 것이 권장되었습니다.

# 예제


#hostingforum.kr
php

// PHP 5.3 이전 버전

class MyClass {

    function __construct($name) {

        $this->name = $name;

    }

}



// PHP 7.2 이후 버전

class MyClass {

    private $name;



    public function __construct($name) {

        $this->name = $name;

    }



    public function getName() {

        return $this->name;

    }

}



// PHP 8.0 이후 버전

class MyClass {

    private $name;



    public function __construct($name) {

        $this->name = $name;

    }



    public function getName() {

        return $this->name;

    }

}



# deprecated 에러 발생


#hostingforum.kr
php

// deprecated 에러 발생

class MyClass {

    function __construct($name) {

        $this->name = $name;

    }

}



$obj = new MyClass('John'); // deprecated 에러 발생



# 해결 방법


#hostingforum.kr
php

// 해결 방법

class MyClass {

    private $name;



    public function __construct($name) {

        $this->name = $name;

    }



    public function getName() {

        return $this->name;

    }

}



$obj = new MyClass('John'); // deprecated 에러 발생하지 않음



# 결론


`__construct` 메소드는 deprecated 상태로 변경되었습니다. PHP 8.0 버전부터 완전히 제거되었습니다. 따라서, PHP 7.2 이후 버전부터는 `__construct` 메소드를 사용하는 대신, `__invoke` 메소드를 사용하는 것이 권장됩니다. 또한, deprecated 에러를 피하기 위해 `__construct` 메소드를 사용하는 대신, 다른 메소드를 사용하는 것이 좋습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 621 페이지

검색

게시물 검색