라이브러리

[PHP] ReflectionClass::getName - 클래스 이름을 가져옵니다.




ReflectionClass::getName


PHP ReflectionClass::getName 메소드는 클래스의 이름을 반환합니다. 이 메소드는 ReflectionClass 객체를 생성할 때 사용할 수 있습니다.

예제


#hostingforum.kr
php

class Person {

    public $name;

    public $age;



    public function __construct($name, $age) {

        $this->name = $name;

        $this->age = $age;

    }

}



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



$reflectionClass = new ReflectionClass('Person');

echo $reflectionClass->getName(); // Person



$reflectionClass = new ReflectionClass($person);

echo $reflectionClass->getName(); // Person



설명


위 예제에서, `ReflectionClass` 객체를 생성할 때 두 가지 방법이 있습니다.

1. 클래스 이름을 문자열로 전달하는 방법: `new ReflectionClass('Person')`
2. 객체를 전달하는 방법: `new ReflectionClass($person)`

`getName` 메소드는 두 경우 모두 클래스 이름을 반환합니다.

사용 사례


`ReflectionClass`는 클래스의 정보를 얻기 위해 사용할 수 있습니다. 예를 들어, 클래스의 속성, 메소드, 상속 관계를 얻을 수 있습니다.

#hostingforum.kr
php

$reflectionClass = new ReflectionClass('Person');

$properties = $reflectionClass->getProperties();

foreach ($properties as $property) {

    echo $property->getName() . "
";

}



이 예제에서는 `Person` 클래스의 속성을 얻어냅니다.

결론


`ReflectionClass::getName` 메소드는 클래스의 이름을 반환합니다. 이 메소드는 클래스의 정보를 얻기 위해 사용할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색