Explain the specific application of PHP5 magic functions. In PHP5 magic function 1 ,__ construct () when instantiating an object, this method of this object is called first. ClassTest {function _ construct () {echobefore ;}}$ t in
PHP5 magic function 1 ,__ construct () when instantiating an object, this method of this object is called first.
- class Test
- {
- function __construct()
- {
- echo "before";
- }
- }
- $t = new Test();
The output is:
Start
We know that the php5 object model and the function with the same class name are class constructor. if we define the constructor and The _ construct () method at the same time, php5 calls the constructor by default instead of The _ construct () function. Therefore, _ construct () is the default constructor of the class.
PHP5 magic function 2 ,__ destruct () this method is called when an object is deleted or the object operation is terminated.
- class Test
- {
- function __destruct()
- {
- echo "end";
- }
- }
- $t = new Test();
Will output
End
We can release resources at the end of the object operation.
PHP5 magic function 3 ,__ get () is called when you try to read an attribute that does not exist.
If you try to read an attribute that does not exist in an object, PHP will give an error message. If the _ get method is added to the class, and we can use this function to implement various operations similar to reflection in java
- Class Test
- {
- Public function _ get ($ key)
- {
- Echo $ key. "nonexistent ";
- }
- }
-
-
- $T=NewTest ();
- Echo $ t->Name;
-
- The output is as follows:
- Name does not exist
PHP5 magic function 4 ,__ set () is called when you try to write a value to an attribute that does not exist.
- Class Test
- {
- Public function _ set ($ key, $ value)
- {
- Echo 'to'. $ key. "value". $ value;
- }
- }
-
-
- $T=NewTest ();
- $ T->Name="Aninggo";
-
- The output is as follows:
- Name value aninggo
PHP5 magic function 5 ,__ call () this method is called when an object does not exist.
- Class Test
- {
- Public function _ call ($ Key, $ Args)
- {
- Echo "the {$ Key} method you want to call does not exist. The parameter you passed in is: ". print_r ($ Args, true );
- }
- }
-
- $T=NewTest ();
- $ T->GetName (aning, go );
The program will output:
The getName method you want to call does not exist. The parameter is Array.
(
[0] => aning
[1] => go
)
PHP5 magic function 6 ,__ toString () is called when an object is printed.
This method is similar to the toString method of java. We call this function when we print the object directly.
- Class Test
- {
- Public function _ toString ()
- {
- Return "print Test ";
- }
- }
-
-
- $T=NewTest ();
-
- Echo $ t;
When echo $ t; is run, $ t->__ toString () is called to output
Print Test
PHP5 magic function 7 ,__ clone () is called when an object is cloned.
- Class Test
- {
-
- Public function _ clone ()
- {
- Echo "I have been copied! ";
- }
- }
-
- $T=NewTest ();
- $T1=Clone$ T;
-
- Program output:
- I have been cloned!
PHP5 magic functions 8. some COOl experimental functions provided by php5
(1) runkit_method_rename
This function dynamically changes the name of the called function.
- Class Test
- {
-
- Function foo (){
- Return "foo! ";
- }
-
- }
-
- Runkit_method_rename (
- 'Test', // class name
- 'Foo', // actually called function
- 'Bar' // display the called function
- );
-
- Echo Test: bar ();
-
- The program will output
-
- Foo!
(2) runkit_method_add
This PHP5 magic function can dynamically add a function to the class.
- Class Test
- {
-
- Function foo (){
- Return "foo! ";
- }
-
- }
-
- Runkit_method_add (
- Test, // class name
- 'Add', // New function name
- '$ Num1, $ num2', // input parameters
- 'Return $ num1 + $ num2; ', // code to be executed
- RUNKIT_ACC_PUBLIC
- );
-
- // Call
- Echo $ e->Add (12, 4 );
(3) runkit_method_copy
You can copy the function in Class A to Class B and rename the PHP5 magic function.
- Class Foo {
- Function example (){
- Return "foo! ";
- }
- }
-
- Class Bar {
- // Empty class
- }
-
- // Execute copy
- Runkit_method_copy ('bar', 'Baz', 'foo', 'example ');
-
- // Execute the copied function
- Echo Bar: baz ();
(4) runkit_method_redefine
Dynamically modify the return value of a function
This PHP5 magic function allows us to easily implement MOCK testing for classes! Is it COOL?
- Class Example {
- Function foo (){
- Return "foo! ";
- }
- }
-
- // Create a test object
- $E=NewExample ();
-
- // Output before the test object
- Echo "Before:". $ e->Foo ();
-
- // Modify the return value
- Runkit_method_redefine (
- 'Example ',
- 'Foo ',
- '',
- 'Return "bar! ";',
- RUNKIT_ACC_PUBLIC
- );
-
- // Execution output
- Echo "After:". $ e->Foo ();
(5) runkit_method_remove
This PHP5 magic function is very simple. you can see it by name and dynamically remove the function from the class.
- Class Test {
- Function foo (){
- Return "foo! ";
- }
-
- Function bar (){
- Return "bar! ";
- }
- }
-
- // Remove the foo function
- Runkit_method_remove (
- 'Test ',
- 'Foo'
- );
-
- Echo implode ('', get_class_methods ('test '));
-
- Program output
- Bar
The code described above is the specific application method of PHP5 magic functions.
PHP5 magic function 1 ,__ construct () when instantiating an object, this method of this object is called first. ClassTest {function _ construct () {echo "before" ;}}$ t =...