오브젝트의 모든 속성을 인쇄하는 방법

php 페이지에 알 수 없는 개체가 있습니다.

인쇄/에코를 통해 어떤 속성/값을 가지고 있는지 확인할 수 있는 방법은 무엇입니까?

기능은 어떨까요?물건에 어떤 기능이 있는지 알 수 있는 방법이 있나요?



질문에 대한 답변



<?php var_dump(obj) ?> 

또는

<?php print_r(obj) ?> 

어레이에서도 같은 것을 사용합니다.

여기에는 PHP 5를 사용하는 개체의 보호된 속성과 개인 속성이 표시됩니다. 정적 클래스 구성원은 설명서에 따라 표시되지 않습니다.

멤버 메서드를 알고 싶은 경우 get_class_methods()를 사용할 수 있습니다.

$class_methods = get_class_methods('myclass'); // or $class_methods = get_class_methods(new myclass()); foreach ($class_methods as $method_name)
{
echo "$method_name<br/>"; } 

관련 정보:

get_object_module()

get_class_class()

get_class() <– 인스턴스 이름




아직 아무도 Reflection API 접근 방식을 제공하지 않았기 때문에 이 방법은 그렇게 될 것 같습니다.

class Person {
public $name = 'Alex Super Tramp';
public $age = 100;
private $property = 'property'; }
$r = new ReflectionClass(new Person); print_r($r->getProperties());
//Outputs Array (
[0] => ReflectionProperty Object
(
 [name] => name
 [class] => Person
)
[1] => ReflectionProperty Object
(
 [name] => age
 [class] => Person
)
[2] => ReflectionProperty Object
(
 [name] => property
 [class] => Person
)
) 

반사를 사용할 때의 이점은 다음과 같이 특성을 기준으로 필터링할 수 있다는 것입니다.

print_r($r->getProperties(ReflectionProperty::IS_PRIVATE)); 

이후 사용자::$property는 프라이빗이며 IS_PRIVATE로 필터링할 때 반환됩니다.

//Outputs Array (
[0] => ReflectionProperty Object
(
 [name] => property
 [class] => Person
)
) 

문서를 읽어보세요!




var_dump($obj);

자세한 내용을 보려면 Reflection Class를 사용할 수 있습니다.

http://www.phpro.org/manual/language.oop5.reflection.html




자세한 내용을 보려면 다음 커스텀 TO($someObject) 함수를 사용합니다.

주어진 객체의 메서드뿐만 아니라 속성, 캡슐화, 릴리즈 노트 등 유용한 정보를 보여주는 간단한 함수를 작성했습니다.

function TO($object){ //Test Object

if(!is_object($object)){

throw new Exception("This is not a Object");

return;

}

if(class_exists(get_class($object), true)) echo "<pre>CLASS NAME = ".get_class($object);

$reflection = new ReflectionClass(get_class($object));

echo "<br />";

echo $reflection->getDocComment();

echo "<br />";

$metody = $reflection->getMethods();

foreach($metody as $key => $value){

echo "<br />". $value;

}

echo "<br />";

$vars = $reflection->getProperties();

foreach($vars as $key => $value){

echo "<br />". $value;

}

echo "</pre>";
 } 

어떻게 동작하는지 보여드리기 위해 랜덤 예시 클래스를 만듭니다.Person이라는 클래스를 만들고 클래스 선언 바로 위에 릴리스 노트를 배치합니다.


/**
* DocNotes -
This is description of this class if given else it will display false
*/
class Person{
 private $name;
 private $dob;
 private $height;
 private $weight;
 private static $num;

function __construct($dbo, $height, $weight, $name) {

$this->dob = $dbo;

$this->height = (integer)$height;

$this->weight = (integer)$weight;

$this->name = $name;

self::$num++;

}
 public function eat($var="", $sar=""){

echo $var;
 }
 public function potrzeba($var =""){

return $var;
 }
} 

이제 사용자의 인스턴스를 만들고 기능을 사용하여 래핑합니다.


$Wictor = new Person("27.04.1987", 170, 70, "Wictor");
TO($Wictor); 

그러면 클래스 이름, 파라미터 및 메서드에 대한 정보(캡슐화 정보 및 파라미터 수 포함), 각 메서드의 파라미터 이름, 메서드 위치 및 존재하는 코드 행이 출력됩니다.다음의 출력을 참조해 주세요.

CLASS NAME = Person /**

* DocNotes -
This is description of this class if given else it will display false

*/
Method [
public method __construct ] {
@@ C:xampphtdocswwwkurs_php_zaawansowanyindex.php 75 - 82
- Parameters [4] {
Parameter #0 [
$dbo ]
Parameter #1 [
$height ]
Parameter #2 [
$weight ]
Parameter #3 [
$name ]
} }
Method [
public method eat ] {
@@ C:xampphtdocswwwkurs_php_zaawansowanyindex.php 83 - 85
- Parameters [2] {
Parameter #0 [
$var = '' ]
Parameter #1 [
$sar = '' ]
} }
Method [
public method potrzeba ] {
@@ C:xampphtdocswwwkurs_php_zaawansowanyindex.php 86 - 88
- Parameters [1] {
Parameter #0 [
$var = '' ]
} }
Property [
private $name ]
Property [
private $dob ]
Property [
private $height ]
Property [
private $weight ]
Property [ private static $num ] 



귀여운 덤프를 사용하여 위대한 나를 위해 일하세요.