라이브러리

[PHP] ReflectionClass::getReflectionConstants - 클래스 상수를 가져옵니다.




ReflectionClass::getReflectionConstants


PHP의 ReflectionClass는 클래스의 정보를 추출하는 데 사용되는 클래스입니다. ReflectionClass::getReflectionConstants 메소드는 클래스의 상수 정보를 반환합니다.

# 사용법


ReflectionClass::getReflectionConstants 메소드는 다음의 형식으로 사용할 수 있습니다.

#hostingforum.kr
php

ReflectionClass::getReflectionConstants($class)



- `$class` : 클래스 이름 또는 클래스 인스턴스

# 예제


#hostingforum.kr
php

// 클래스 정의

class MyClass {

    const MY_CONSTANT = 'Hello, World!';

}



// ReflectionClass 인스턴스 생성

$reflectionClass = new ReflectionClass('MyClass');



// 클래스 상수 정보 추출

$reflectionConstants = $reflectionClass->getReflectionConstants();



// 클래스 상수 정보 출력

foreach ($reflectionConstants as $constant) {

    echo $constant->getName() . ' : ' . $constant->getValue() . "
";

}



# 결과


#hostingforum.kr


MY_CONSTANT : Hello, World!



# 설명


- `ReflectionClass::getReflectionConstants` 메소드는 클래스의 상수 정보를 반환합니다.
- 반환된 정보는 `ReflectionClassConstant` 객체의 배열로 반환됩니다.
- `ReflectionClassConstant` 객체는 상수 이름과 값, 타입을 포함한 정보를 제공합니다.

# 예제 2


#hostingforum.kr
php

// 클래스 정의

class MyClass {

    const MY_CONSTANT1 = 10;

    const MY_CONSTANT2 = 20;

}



// ReflectionClass 인스턴스 생성

$reflectionClass = new ReflectionClass('MyClass');



// 클래스 상수 정보 추출

$reflectionConstants = $reflectionClass->getReflectionConstants();



// 클래스 상수 정보 출력

foreach ($reflectionConstants as $constant) {

    echo $constant->getName() . ' : ' . $constant->getValue() . "
";

}



# 결과


#hostingforum.kr


MY_CONSTANT1 : 10

MY_CONSTANT2 : 20



# 설명


- `ReflectionClass::getReflectionConstants` 메소드는 클래스의 상수 정보를 반환합니다.
- 반환된 정보는 `ReflectionClassConstant` 객체의 배열로 반환됩니다.
- `ReflectionClassConstant` 객체는 상수 이름과 값, 타입을 포함한 정보를 제공합니다.

# 예제 3


#hostingforum.kr
php

// 클래스 정의

class MyClass {

    const MY_CONSTANT = 'Hello, World!';

}



// ReflectionClass 인스턴스 생성

$reflectionClass = new ReflectionClass('MyClass');



// 클래스 상수 정보 추출

$reflectionConstants = $reflectionClass->getReflectionConstants();



// 클래스 상수 정보 출력

foreach ($reflectionConstants as $constant) {

    echo $constant->getName() . ' : ' . $constant->getValue() . "
";

    echo '타입 : ' . gettype($constant->getValue()) . "
";

}



# 결과


#hostingforum.kr


MY_CONSTANT : Hello, World!

타입 : string



# 설명


- `ReflectionClass::getReflectionConstants` 메소드는 클래스의 상수 정보를 반환합니다.
- 반환된 정보는 `ReflectionClassConstant` 객체의 배열로 반환됩니다.
- `ReflectionClassConstant` 객체는 상수 이름과 값, 타입을 포함한 정보를 제공합니다.
- `gettype` 함수를 사용하여 상수 값의 타입을 확인할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색