2015-01-30 2 views
-1

클래스의 인스턴스를 만들지 않고 클래스에서 정적 속성을 가져 오는 방법이 있습니까?PHP 클래스에서 통계 가져 오기

$reflection = new ReflectionObject('Foo'); 
$staticProperties = $reflection->getStaticProperties(); 

이렇게하면 오류가 발생합니다.

ReflectionObject::__construct() expects parameter 1 to be object, string given in test.php on line 19 

이 워드 프로세서 내가 문자열을 전달 할 수 있어야 보여줄 것 5.5 PHP ..

http://php.net/manual/en/reflectionclass.construct.php

Either a string containing the name of the class to reflect, or an object. 

어떤 생각인가? 토큰을 파싱하지 않고도 파일을 가져오고 싶습니다.

+6

대신 'ReflectionClass'을 사용하십시오. – PeeHaa

+0

권자. 잠을 자지 못하면서 ... – Wizzard

답변

3

@PeeHaa와 마찬가지로, ReflectionObjectReflectionClass이 아닙니다.

새로운 코드 :

$reflection = new ReflectionClass('Foo'); 
$staticProperties = $reflection->getStaticProperties(); 
0

ReflectionObject는 개체 인스턴스에 대한 정보를 제공합니다. 귀하가 찾고있는 질문은 ReflectionClass입니다.

0

이 링크는 PHP 레퍼런스에서 직접 가져온 것입니다 http://php.net/manual/en/reflectionclass.getstaticpropertyvalue.php

<?php 
class Apple { 
    public static $color = 'Red'; 
} 

$class = new ReflectionClass('Apple'); 
var_dump($class->getStaticPropertyValue('color')); 
?> 

을 그래서 여기에 당신이 클래스와 정적 속성의 이름 (요청 문자열)

0

을 필요로 볼 수 있습니다 시도 :

ClassName::propertyName 

를 여기있다 :

ReflectionObject::getStaticProperties()