In
PHP5 Magic Function 1,__construct () when an object is instantiated, this method of the 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 same class name function are the constructors of the class, so if we define both the constructor and the __construct () method, PHP5 will call the constructor by default instead of calling the __construct () function, so __construct () As the default constructor for a class
PHP5 Magic Function 2,__destruct () invokes the method when an object or object operation is terminated.
- Class Test
- {
- function __destruct ()
- {
- echo "End";
- }
- }
- $ T New
will be output
End
We can do things like freeing resources at the end of an object operation.
PHP5 Magic Function 3,__get () is called when attempting to read a property that does not exist.
If you try to read a property that does not exist for an object, PHP will give you an error message. If you add a __get method to a class, and we can use this function to implement various actions like reflection in Java
- Class Test
- {
- Public Function __get ($key)
- {
- Echo $key. "does not exist";
- }
- }
- $ T New Test ();
- Echo $t- > name;
- It will output:
- Name does not exist
PHP5 Magic Function 4,__set () is called when attempting to write a value to a property that does not exist.
- class Test
- {
- public function __set ($key, $value)
- {
- Echo ' pair '. $key. "Attached value". $value;
- }
- }
-
-
- $ t = new Test ();
- $t- ; name = "Aninggo" ;
-
- output:
- to name appended value Aninggo
PHP5 Magic Function 5,__call () Call this method when attempting to invoke a method that does not exist for an object.
- Class Test
- {
- Public Function __call ($Key, $Args)
- {
- echo "The {$Key} method you are calling does not exist. The parameter you passed in is: ". Print_r ($Args, true);
- }
- }
- $ T New Test ();
- $t- > GetName (Aning,go);
The program will output:
The GetName method you are calling does not exist. parameter is: Array
(
[0] = aning
[1] = = Go
)
PHP5 Magic Function 6,__tostring () is called when printing an object
This method is similar to the ToString method of Java, when we directly print the object callback with this function
- Class Test
- {
- Public Function __tostring ()
- {
- Return "Print Test";
- }
- }
- $ T New Test ();
- Echo $t;
When the Echo $t is run, $t->__tostring () is called and the output
Print Test
PHP5 Magic Function 7,__clone () is called when the object is cloned
- Class Test
- {
- Public Function __clone ()
- {
- echo "I've been copied!" ";
- }
- }
- $ T New Test ();
- $ T1 clone $t;
- Program output:
PHP5 Magic function 8. By the way, some of the very cool experimental functions provided in PHP5 are
(1) runkit_method_rename
This function can dynamically change the name of the function we call.
- class Test
- {
-
- function foo () {
- return "Foo!";
- }
-
- }
-
- runkit_method_rename (
- < span> ' Test ',//class name
- ' foo ',//function actually called
- ' Ba R '//display called function
-
-
- Echo Test::bar ();
-
- program outputs
-
- foo!
(2) Runkit_method_add
This PHP5 magic function can dynamically add functions to a class
- Class Test
- {
- function foo () {
- return "Foo!";
- }
- }
- Runkit_method_add (
- Test,//class name
- ' Add ',//new function name
- ' $num 1, $num 2 ',//parameters passed in
- ' Return $num 1 + $num 2; ',//code executed
- Runkit_acc_public
- );
- Call
- Echo $e- > Add (4);
(3) runkit_method_copy
You can copy the functions 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 ');
- function after the copy is executed
- Echo Bar::baz ();
(4) Runkit_method_redefine
Dynamically changing the return value of a function
This PHP5 magic function allows us to easily implement mock tests of classes! Isn't it cool?
- class Example {
- function foo () {
- return "foo!";
- }
- }
- //Create a Test object
- $ e = New Example ();
- //output before testing the object
- echo "Before:". $e->foo ();
- //Modify 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, see the name can be seen, the dynamic removal of the function from the class
- class Test {
- function foo () {
- return "Foo!";
- }
-
- Fun Ction Bar () {
- return "bar!";
- }
- }
- li>
- //remove foo function
- Runkit_method_remove (
- ' Test ',
- ' foo '
- );
-
- Echo implode (', Get_class_methods (' Test '));
-
- program output
- bar
The code described above is about how the PHP5 magic function is applied.
http://www.bkjia.com/PHPjc/446384.html www.bkjia.com true http://www.bkjia.com/PHPjc/446384.html techarticle in PHP5 Magic function 1,__construct () When instantiating an object, this method of the object is called first. classtest {function__construct () {echo "Before";}} $ t = ...