PHP classes and object function instances

Source: Internet
Author: User

1. interface_exists, class_exists, method_exists, and property_exists: as the name suggests, the functions of these functions can be guessed from the names of the above functions. I think that's why I like this programming language more and more with my deep learning of PHP. Below we will give a brief description of their prototype. For more information, see the sample code. Bool interface_exists (string $ interface_name [, bool $ autoload = true]) determines whether an interface exists, and the second parameter indicates whether to execute _ autoload during search. Bool class_exists (string $ class_name [, bool $ autoload = true]) determines whether the class exists. The second parameter indicates whether to execute _ autoload during the search. 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 the specified class or object contains the specified member variable. <? Php // in another_test_class.phpinterface AnotherTestInterface {} class AnotherTestClass {public static function printMe () {print "This is Test2: printSelf. \ n ";}public function doSomething () {print" This is Test2: doSomething. \ n ";} public function doSomethingWithArgs ($ arg1, $ arg2) {print 'this is Test2: doSomethingWithArgs with ($ arg1 = '. $ arg1. 'and $ arg2 = '. $ arg2 ."). \ n ";}}<? Php // in class_exist_test.php. The classes and interfaces required in the test code are located in another_test_class.php. // The rule can be found that the names of classes and interfaces are in the camper style, words in a file name are separated by underscores. // Two _ autoload methods are provided here. Because the first method is more common and convenient, We comment out the second method here. You can view manual for the differences between them. 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 ). ". ph P "; //}); 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 ('anothertestinter', false) {print "This interface doesn' t exist if no autoload. \ n ";} print" \ nThe following case is tested after executing autoload. \ n "; if (class_exists ('anothertestclass', true) {print" This class exists if autoload is set to true. \ n ";} if (interface_exists ('anothertestinterface', true) {print" This interface exists if autoload is set to true. \ n ";}the running result is as follows: B Ogon: 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 is tested after executing autoload. this class exists if autoload is set to true. 2. get_declared_classes and get_declared_interfaces: return all currently accessible classes and interfaces, including not only custom classes and interfaces, but also PHP built-in classes and interfaces. Their function declaration is very simple, without parameters, but returns an array. See the following code: <? Phpinterface AnotherTestInterface {} class AnotherTestClass {public static function printMe () {print "This is Test2: printSelf. \ n ";}} print_r (get_declared_interfaces (); print_r (get_declared_classes (); because the output result is too long, and the two functions are relatively simple, so the output result is no longer given below. 3. get_class_methods, get_class_vars, and get_object_vars: the three functions have one thing in common, that is, they can only obtain all member functions, member variables, or non-static member variables within the visible scope of the scope. For example, if a class is called internally, all member functions or variables meet the conditions. Outside the class, only common functions and variables can be returned. Array get_class_methods (mixed $ class_name) gets the member functions that can be accessed in the specified class. Array get_class_vars (string $ class_name) gets the member variables that can be accessed in the specified class. Array get_object_vars (object $ object) obtains the non-static member variables that can be accessed. <? Phpfunction 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 () {outpu T_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 running result is 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 = publicFunctionget_class_vars ..................... $ key = publicVar => $ value = 1 $ key = pri VateVar => $ 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 = publicFunctionget_class_vars ..................... $ key = publicVar =>$ value = 1 $ key = staticPublicVar => $ Value = get_object_vars ..................... $ key = publicVar => $ value = 1 4. get_called_class and get_class: string get_class ([object $ object = NULL]) Get the Class Name of the parameter object. String get_called_class (void) the name of the current class when the static method is called. <? Phpclass Base {static public function test () {var_dump (get_called_class () ;}} class Derive extends Base {} Base: test (); Derive: test (); var_dump (get_class (new Base (); var_dump (get_class (new Derive (); the running result is 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 related to class inheritance, so I put them together. String get_parent_class ([mixed $ object]) obtains the parent class of the parameter object. If no parent class exists, false is returned. 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 subclass of $ class_name. <? Phpclass Base {static public function test () {var_dump (get_called_class () ;}} class Derive extends Base {} var_dump (get_parent_class (new Derive ())); var_dump (is_a (new Derive (), '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 running result is as follows: bogon: TestPhp $ php another_test_class.php string (4) "Base" bool (true) 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.