Introduction to PHP interface_exists, class_exists, method_exists, and property_exists

Source: Internet
Author: User

1. Interface_exists, Class_exists, method_exists and property_exists:

As the name suggests, you can guess a few of their functions from the names of the above functions. I think that's why I love this programming language more and more as I learn more about PHP. Let's give them a prototype statement and a short description, and more to see the example code directly.
BOOL Interface_exists (String $interface _name [, bool $autoload = True]) determines whether an interface exists, and the second parameter indicates whether the __autoload is executed when the lookup occurs.
BOOL Class_exists (String $class _name [, bool $autoload = True]) determines whether a class exists, and the second parameter indicates whether the __autoload is executed when the lookup occurs.
the bool Method_exists (mixed $object, string $method _name) determines whether the specified class or object contains the specified member function.
BOOL Property_exists (mixed $class, string $property) determines whether a specified class or object contains a specified member variable.

The code is as follows Copy Code


<?php
In another_test_class.php
Interface Anothertestinterface {

}

Class Anothertestclass {
    public static function Printme () {
         print "This is Test2::p RINTSELF.N";
   }
    Public Function dosomething () {
        print ' This is Test2::d OSOMETHING.N ";
   }
    Public Function Dosomethingwithargs ($arg 1, $arg 2) {
      & nbsp print ' This is Test2::d Osomethingwithargs with ($arg 1 = '. $arg 1. ' and $arg 2 = '. $arg 2. "). n ";
   }
}

<?php
In class_exist_test.php, the classes and interfaces required in the following test code are located in Another_test_class.php,
It is possible to find patterns, the names of classes and interfaces are hump-style, and the names of the words are separated by underscores.
Here are two ways to __autoload, because the first one is more common and convenient, so we will comment out the second way, the difference between them can be viewed manual.
function __autoload ($classname) {
$nomilizedClassname = Strtolower (Preg_replace ('/a-z]w*) ([a-z]w*) ([a-z]w*)/', ' ${1}_${2}_${3} ', $classname));
Require Strtolower ($nomilizedClassname). ". PHP ";
}
Spl_autoload_register (function ($classname) {
$nomilizedClassname = Strtolower (Preg_replace ('/a-z]w*) ([a-z]w*) ([a-z]w*)/', ' ${1}_${2}_${3} ', $classname));
Require Strtolower ($nomilizedClassname). ". PHP ";
//});

Print "The following case is tested before executing AUTOLOAD.N";
if (!class_exists (' Anothertestclass ', false)) {
Print "This class doesn ' t exist if no AUTOLOAD.N";
}

if (!interface_exists (' Anothertestinterface ', false)) {
Print "This interface doesn ' t exist if no AUTOLOAD.N";
}

Print "nthe following case are tested after executing AUTOLOAD.N";
if (class_exists (' Anothertestclass ', true)) {
Print "This class exists if autoload be set to TRUE.N";
}

if (interface_exists (' Anothertestinterface ', true)) {
Print "This interface exists if autoload are set to TRUE.N";
}

The results of the operation are as follows:


bogon:testphp$ PHP class_exist_test.php
The following case is tested before executing autoload.
This class doesn ' t exist if no autoload.
This interface doesn ' t exist if no autoload.

The following case was tested after executing autoload.
This class exists if autoload are set to true.
This interface exists if autoload are set to TRUE.2. Get_declared_classes and Get_declared_interfaces:

Returns all classes and interfaces that are currently accessible, including not only custom classes and interfaces, but also PHP-built classes and interfaces. Their function declarations are very simple, with no arguments, just returning an array. See the following code:

The code is as follows Copy Code


<?php
Interface Anothertestinterface {

}

Class Anothertestclass {
public static function Printme () {
Print "This is Test2::p RINTSELF.N";
}
}

Print_r (Get_declared_interfaces ());
Print_r (Get_declared_classes ());

Because the output is too long and the two functions are simpler, the output is no longer shown below.

3. Get_class_methods, Get_class_vars and Get_object_vars:

These three functions have one thing in common, that is, you can only get all member functions, member variables, or non-static member variables within the scope that are visible to the scopes. For example, when called inside a class, all member functions or variables are eligible, and outside of the class, only common functions and variables can be returned.
Array get_class_methods (mixed $class _name) Gets the member functions that are accessible in the specified class.
Array Get_class_vars (string $class _name) Gets the member variables that are accessible in the specified class.
The array get_object_vars (object $object) gets the non-static member variables that can be accessed.

The code is as follows Copy Code


<?php
function Output_array ($functionName, $items) {
Print "$functionName ............. ... n.
foreach ($items as $key => $value) {
print ' $key = '. $key. ' => $value = '. $value. n ";
}
}

Class TestClass {
public $publicVar = 1;
Private $privateVar = 2;
static private $staticPrivateVar = "Hello";
static public $staticPublicVar;

Private Function Privatefunction () {

}
function Publicfunction () {
Output_array ("Get_class_methods", Get_class_methods (__class__));
Output_array (' Get_class_vars ', Get_class_vars (__class__));
Output_array (' Get_object_vars ', Get_object_vars ($this));
}
}

$TESTOBJ = new TestClass ();
Print "The following is output within TESTCLASS.N";
$TESTOBJ->publicfunction ();

Print "nthe following is output out of TESTCLASS.N";
Output_array (' Get_class_methods ', get_class_methods (' TestClass '));
Output_array (' Get_class_vars ', Get_class_vars (' TestClass '));
Output_array (' Get_object_vars ', Get_object_vars ($TESTOBJ)); The results of the operation are as follows:


bogon:testphp liulei$ PHP class_exist_test.php
The following is output within TestClass.
Get_class_methods ..............
$key = 0 => $value = privatefunction
$key = 1 => $value = publicfunction
Get_class_vars ..............
$key = Publicvar => $value = 1
$key = Privatevar => $value = 2
$key = Staticprivatevar => $value = Hello
$key = Staticpublicvar => $value =
Get_object_vars ..............
$key = Publicvar => $value = 1
$key = Privatevar => $value = 2

The following is output out of TestClass.
Get_class_methods ..............
$key = 0 => $value = publicfunction
Get_class_vars ..............
$key = Publicvar => $value = 1
$key = Staticpublicvar => $value =
Get_object_vars ..............
$key = Publicvar => $value = 14. Get_called_class and Get_class:

String Get_class ([Object $object = NULL]) Www.111Cn.net gets the class name of the Parameter object.
The current class name at the time of a string get_called_class (void) static method call.


<?php
Class Base {
static public Function test () {
Var_dump (Get_called_class ());
}
}

Class Derive extends Base {
}

Base::test ();
Derive::test ();

Var_dump (New Base ()) (Get_class);
Var_dump (Get_class (new Derive ()));

The results of the operation are as follows:

bogon:testphp$ PHP another_test_class.php
String (4) "Base"
String (6) "derive"
String (4) "Base"
String (6) "derive"

5. Get_parent_class, Is_a and is_subclass_of:

These three functions are all related to the inheritance of the class, so I put them together.

String Get_parent_class ([mixed $object]) gets the parent class of the Parameter object and returns False if there is no parent class.
the bool Is_a (object $object, String $class _name) determines whether the first parameter object is an object of the $class_name class itself or its parent class.
BOOL Is_subclass_of (mixed $object, string $class _name) determines whether the first parameter object is a $class_name subclass.

The code is as follows Copy Code

<?php
Class Base {
    static public Function test () {
        V Ar_dump (Get_called_class ());
   }
}

Class derive extends Base {
}

Var_dump (Get_parent_class (new Derive ());
Var_dump (Is_a (New De Rive (), ' derive ');
Var_dump (Is_a (new Derive (), ' Base ');
Var_dump (Is_a () (New Base (), ' derive ');

Var_dump (Is_subclass_of (new Derive (), ' derive '));
Var_dump (is_subclass_of (new Derive (), ' Base ');    the results of the operation are as follows:


bogon:testphp$ php another_test_class.php
String (4) "Base"
bool (TRUE)
bool (TRUE)
bool (FALSE)
bool (FALSE)
bool (true)

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.