
ReflectionObject::__construct는 클래스의 생성자 메서드를 반환하는 메서드입니다. 클래스를 생성할 때 자동으로 호출되는 메서드는 __construct가 아니고, new 키워드를 사용하여 클래스를 인스턴스화할 때 자동으로 호출됩니다.
예를 들어, 다음 코드에서 TestClass 클래스의 인스턴스를 생성할 때 __construct 메서드가 호출됩니다.
#hostingforum.kr
php
class TestClass {
public function __construct($name) {
echo "TestClass 생성";
}
}
$reflection = new ReflectionClass('TestClass');
$reflection->newInstance('이름을 넣어');
위 코드에서 $reflection->newInstance()는 TestClass 클래스의 인스턴스를 생성할 때 자동으로 __construct 메서드가 호출되는 것이 아닙니다. __construct 메서드는 new 키워드를 사용하여 클래스를 인스턴스화할 때 자동으로 호출됩니다.
#hostingforum.kr
php
$testClass = new TestClass('이름을 넣어');
위 코드에서 TestClass 클래스의 인스턴스를 생성할 때 __construct 메서드가 호출됩니다.
__construct 메서드를 호출하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$reflection = new ReflectionClass('TestClass');
$reflection->newInstance('이름을 넣어');
또는
#hostingforum.kr
php
$testClass = $reflection->newInstance('이름을 넣어');
또는
#hostingforum.kr
php
$reflection = new ReflectionClass('TestClass');
$reflection->newInstanceArgs(array('이름을 넣어'));
__construct 메서드에 대한 더 많은 정보는 PHP의 생성자 메서드에 대한 설명을 참고하시기 바랍니다.
2025-04-11 18:44