라이브러리

[PHP] ReflectionClass::newInstanceWithoutConstructor - 생성자를 호출하지 않고 새 클래스 인스턴스를 생성합니다.




ReflectionClass::newInstanceWithoutConstructor


PHP의 ReflectionClass는 클래스의 정보를 가져올 수 있는 클래스입니다. ReflectionClass::newInstanceWithoutConstructor는 클래스의 인스턴스를 생성하는 메소드입니다. 이 메소드는 클래스의 생성자 함수를 호출하지 않고 클래스의 인스턴스를 생성합니다.

사용법


ReflectionClass::newInstanceWithoutConstructor는 다음과 같은 형식으로 사용할 수 있습니다.

#hostingforum.kr
php

$reflectionClass = new ReflectionClass('클래스 이름');

$reflectionClass->newInstanceWithoutConstructor();



예제


다음 예제는 ReflectionClass::newInstanceWithoutConstructor를 사용하여 클래스의 인스턴스를 생성하는 방법을 보여줍니다.

#hostingforum.kr
php

class Person {

    public $name;

    public $age;



    public function __construct($name, $age) {

        $this->name = $name;

        $this->age = $age;

    }

}



$reflectionClass = new ReflectionClass('Person');

$person = $reflectionClass->newInstanceWithoutConstructor();



$person->name = 'John';

$person->age = 30;



print($person->name . "
"); // John

print($person->age . "
"); // 30



주의점


ReflectionClass::newInstanceWithoutConstructor는 클래스의 생성자 함수를 호출하지 않으므로, 클래스의 인스턴스 변수를 초기화하지 않습니다. 따라서, 클래스의 인스턴스 변수를 초기화하려면, 인스턴스 변수를 직접 초기화해야 합니다.

사용 이유


ReflectionClass::newInstanceWithoutConstructor는 클래스의 인스턴스를 생성하는 데 사용할 수 있습니다. 예를 들어, 클래스의 인스턴스를 생성하여 데이터를 저장하는 데 사용할 수 있습니다.

결론


ReflectionClass::newInstanceWithoutConstructor는 클래스의 인스턴스를 생성하는 데 사용할 수 있는 메소드입니다. 이 메소드는 클래스의 생성자 함수를 호출하지 않으므로, 클래스의 인스턴스 변수를 직접 초기화해야 합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색