PHP Reflection function

Source: Internet
Author: User
Some of the most commonly used PHP reflection functions Get_class Get_class_methods

1. Get_class? Returns the class name of the object

String Get_class ([object $obj])

Returns the name of the class to which the object instance obj belongs. Returns FALSE if obj is not an object.

Note: Classes defined in the PHP extension library return the names of their original definitions. In PHP 4, Get_class () returns the lowercase form of a user-defined class name, but in PHP 5 the name of the class name is returned as if it were the class name in the extension library.

Note: Since PHP 5, obj is optional if called in the method of the object.

Example#1 using Get_class ()


class Foo {
function foo ()
{
Implements some logic
}

Function name ()
{
echo "My name is", Get_class ($this), "/n";
}
}

Create an Object
$bar = new Foo ();

External call
echo "Its name is", Get_class ($bar), "/n";

Internal call
$bar->name ();

?>

The example above will output:

Its name was Foo My name is foo
2.get_class_methods? Returns an array of the method names of the class
Description

Array get_class_methods (mixed $class _name)

Returns an array of the method names that are defined in the class specified by Class_name. If an error occurs, NULL is returned.

Example#1 Get_class_methods () example


class MyClass {
Constructor
function MyClass ()
{
return (true);
}

Method 1
function Myfunc1 ()
{
return (true);
}

Method 2
function Myfunc2 ()
{
return (true);
}
}

$class _methods = get_class_methods (' MyClass ');
Or
$class _methods = get_class_methods (new MyClass ());

foreach ($class _methods as $method _name) {
echo "$method _name/n";
}

?>

The example above will output:

3.

Get_class_vars? Returns an array that consists of the default properties of the class

Description

Array Get_class_vars (string $class _name)

Returns an associative array of the default public properties of the class, with elements of this array in the form of varname = = value.

Note: Before PHP 4.2.0, Get_class_vars () will not contain uninitialized class variables.

Example#1 Get_class_vars () example


class MyClass {

var $var 1; There is no default value for this variable ...
var $var 2 = "xyz";
var $var 3 = 100;
Private $var 4; PHP 5

Constructor
function MyClass () {
Change some properties
$this->var1 = "Foo";
$this->var2 = "Bar";
return true;
}

}

$my _class = new MyClass ();

$class _vars = Get_class_vars (Get_class ($my _class));

foreach ($class _vars as $name = + $value) {
echo "$name: $value/n";
}

?>

The example above will output:

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.