라이브러리

[PHP] ReflectionConstant::__toString - 문자열 표현을 반환합니다.




ReflectionConstant::__toString


PHP의 ReflectionClass와 ReflectionMethod, ReflectionProperty와 함께 ReflectionConstant도 제공됩니다. ReflectionConstant는 PHP의 상수에 대한 정보를 제공합니다.

ReflectionConstant::__toString 메소드는 ReflectionConstant의 값을 문자열로 반환합니다.

예제


#hostingforum.kr
php

class MyClass {

    const MY_CONSTANT = 'Hello, World!';

}



$reflectionClass = new ReflectionClass('MyClass');

$reflectionConstant = $reflectionClass->getConstant('MY_CONSTANT');



echo $reflectionConstant->getName() . "
"; // MY_CONSTANT

echo $reflectionConstant->getValue() . "
"; // Hello, World!

echo $reflectionConstant->__toString() . "
"; // Hello, World!



ReflectionConstant의 사용 방법


ReflectionConstant는 PHP의 상수에 대한 정보를 제공합니다. ReflectionConstant를 사용하여 상수의 이름, 값, 그리고 타입을 확인할 수 있습니다.

ReflectionConstant의 속성


- `getName()`: 상수의 이름을 반환합니다.
- `getValue()`: 상수의 값을 반환합니다.
- `getType()`: 상수의 타입을 반환합니다.
- `__toString()`: 상수의 값을 문자열로 반환합니다.

ReflectionConstant의 메소드


- `getName()`: 상수의 이름을 반환합니다.
- `getValue()`: 상수의 값을 반환합니다.
- `getType()`: 상수의 타입을 반환합니다.
- `__toString()`: 상수의 값을 문자열로 반환합니다.

ReflectionConstant의 예제


#hostingforum.kr
php

class MyClass {

    const MY_CONSTANT = 'Hello, World!';

    const MY_CONSTANT_INT = 123;

    const MY_CONSTANT_FLOAT = 123.456;

    const MY_CONSTANT_BOOL = true;

}



$reflectionClass = new ReflectionClass('MyClass');

$reflectionConstant = $reflectionClass->getConstant('MY_CONSTANT');



echo $reflectionConstant->getName() . "
"; // MY_CONSTANT

echo $reflectionConstant->getValue() . "
"; // Hello, World!

echo $reflectionConstant->getType() . "
"; // string



$reflectionConstant = $reflectionClass->getConstant('MY_CONSTANT_INT');



echo $reflectionConstant->getName() . "
"; // MY_CONSTANT_INT

echo $reflectionConstant->getValue() . "
"; // 123

echo $reflectionConstant->getType() . "
"; // int



$reflectionConstant = $reflectionClass->getConstant('MY_CONSTANT_FLOAT');



echo $reflectionConstant->getName() . "
"; // MY_CONSTANT_FLOAT

echo $reflectionConstant->getValue() . "
"; // 123.456

echo $reflectionConstant->getType() . "
"; // double



$reflectionConstant = $reflectionClass->getConstant('MY_CONSTANT_BOOL');



echo $reflectionConstant->getName() . "
"; // MY_CONSTANT_BOOL

echo $reflectionConstant->getValue() . "
"; // 1

echo $reflectionConstant->getType() . "
"; // bool



ReflectionConstant의 사용 시 주의사항


- ReflectionConstant는 PHP의 상수에 대한 정보를 제공합니다. 따라서 상수가 존재하지 않으면 ReflectionConstant의 속성과 메소드는 null을 반환합니다.
- ReflectionConstant는 상수의 타입을 반환합니다. 따라서 상수가 정의되지 않은 타입을 가질 수 있습니다.
- ReflectionConstant는 상수의 값을 반환합니다. 따라서 상수가 존재하지 않으면 ReflectionConstant의 속성과 메소드는 null을 반환합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색