라이브러리

[PHP] ReflectionClass::getConstructor - 클래스 생성자를 가져옵니다.




ReflectionClass::getConstructor


PHP ReflectionClass::getConstructor 메소드는 클래스의 생성자 메소드를 반환합니다. 생성자 메소드는 클래스가 생성될 때 호출되는 메소드입니다.

예제


#hostingforum.kr
php

class Person {

    private $name;

    private $age;



    public function __construct($name, $age) {

        $this->name = $name;

        $this->age = $age;

    }



    public function getName() {

        return $this->name;

    }



    public function getAge() {

        return $this->age;

    }

}



$reflectionClass = new ReflectionClass('Person');

$constructor = $reflectionClass->getConstructor();



echo "생성자 메소드 이름: " . $constructor->getName() . "
";

echo "생성자 메소드 매개변수: " . implode(', ', $constructor->getParameters()) . "
";



$person = new Person('John Doe', 30);

echo "이름: " . $person->getName() . "
";

echo "나이: " . $person->getAge() . "
";



결과


#hostingforum.kr


생성자 메소드 이름: __construct

생성자 메소드 매개변수: name, age

이름: John Doe

나이: 30



설명


위 예제에서, `Person` 클래스의 생성자 메소드는 `__construct` 메소드입니다. 이 메소드는 `name`과 `age` 매개변수를 받습니다. `ReflectionClass::getConstructor` 메소드를 사용하여 `Person` 클래스의 생성자 메소드를 얻은 후, `getName` 메소드를 사용하여 생성자 메소드 이름을 얻고, `getParameters` 메소드를 사용하여 생성자 메소드 매개변수를 얻습니다.

사용 방법


`ReflectionClass::getConstructor` 메소드를 사용하여 클래스의 생성자 메소드를 얻을 수 있습니다. 생성자 메소드는 클래스가 생성될 때 호출되므로, 클래스의 생성자 메소드를 얻을 때는 클래스가 생성된 후에 사용해야 합니다.

참고


* [PHP ReflectionClass](https://www.php.net/manual/kr/class.reflectionclass.php)
* [PHP ReflectionClass::getConstructor](https://www.php.net/manual/kr/reflectionclass.getconstructor.php)
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색