PHP class and object function example _ PHP Tutorial

Source: Internet
Author: User
Describes PHP classes and object function instances. 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 this is also my example with 1. interface_exists, class_exists, method_exists, and property_exists:

As the name suggests, you can guess a few points of their functions 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.

// In another_test_class.php

Interface 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 ";

}

}

// In class_exist_test.php, the classes and interfaces required in the following test code are located in another_test_class.php,

// The rule can be found that the names of classes and interfaces are camper, and words in file names 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). ". 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 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:

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 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:

Interface 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, 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.

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 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 = 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 = 1

4. get_called_class and get_class:

String get_class ([object $ object = NULL]) obtains the class name of the parameter object.

String get_called_class (void) the name of the current class when the static method is called.

Class 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.

Class 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 (true)

Bool (false)

Bool (false)

Bool (true)

Http://www.bkjia.com/PHPjc/678024.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/678024.htmlTechArticle1. interface_exists, class_exists, method_exists, and property_exists: As the name suggests, from the names of the above functions you can guess a few points of their functionality. I think this is what I followed...

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.