Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How can I look inside an object or class to see its structure in Php?

user-image
Question ajoutée par Afsheen Atif
Date de publication: 2014/06/07
Julfkar  Moh Umar
par Julfkar Moh Umar , Sr. Software Engineer , Aakash Edutech Private Limited

You have to use var_dump() method for this.

Afsheen Atif
par Afsheen Atif

Since PHP5.0, PHP has included a reflection API, which lets us peer into any class or object and obtain detailed information on its properties, methods, interfaces,and constants. To use it, instantiate an object of either ReflectionClass or ReflectionObject and pass it the class or object to be X-rayed.

Here’s an example:

<?php

 

class abc {

            public $prop;

           

            public function __construct(){

            echo 'Creating a new ' . get_class($this) . '...';

            }

            public function getname($name){

            $this->prop = $name;

            }

 

            public function showname() {

 

            return $this->prop;

            }

}

 

class xyz extends abc {

 

            public function __construct(){

            parent::__construct();

            }

}

 

$obj = new abc();

Reflection::export(new ReflectionClass($obj));

 ?>

Its result is:

Creating a new abc...Class [ class abc ] { @@ C:\\wamp\\www\\abc.php3-17 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [1] { Property [ public $prop ] } - Methods [3] { Method [<user, ctor="">public method __construct ] { @@ C:\\wamp\\www\\abc.php6 -8 } Method [ public method getname ] { @@ C:\\wamp\\www\\abc.php9 -11 - Parameters [1] { Parameter #0 [ $name ] } } Method [ public method showname ] { @@ C:\\wamp\\www\\abc.php13 -16 } } }

 

Cheers!

Muhammad Majid Saleem
par Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

I couldn't get your question completely. But I am replying as per my understanding. You may see an object's structure using following fuction:

$a_obj = new a();

var_dump( $a_obj );

 

 

More Questions Like This