Dynamic Call instance analysis in php, php call instance Analysis _ PHP Tutorial

Source: Internet
Author: User
Dynamic Call instance analysis in php and php call instance analysis. Dynamic Call instance analysis in php. php call instance analysis this article describes the specific usage of Dynamic Call in php. Share it with you for your reference. The specific analysis is as follows: Dynamic Call instance analysis in php in the program, and php call instance analysis

This example describes the usage of dynamic calling in php. Share it with you for your reference. The specific analysis is as follows:

Adding a lot of judgments in the program is indeed a great headache, for example:

The code is as follows:

If ($ fun = 'a') {echo "Oh! ";}
Elesif (){}
......
Else {echo "well! ";}


This is really troublesome and will cause a great deal of trouble in later reading and modifying the program. at this time, we can use the function to implement every code segment to be executed, then we can use a more NB method to implement these functions, and because each function implements a function, we will be much simpler to maintain.

Go to the topic and check out what the function can be called dynamically in PHP, such as $ fun (), the PHP parser can call the pair function based on the value of the variable $ fun. for example, $ fun = 'A', the parser will see, to call function a, the specific code is as follows:

The code is as follows:

<? Php
// Controller. php
(Isset ($ _ GET ['fun ']) & $ _ GET ['fun']! = '')? $ Fun = $ _ GET ['fun ']: $ fun = 'Def ';

Controller ($ fun );

Function controller ($ fun ){
If (function_exists ($ fun) $ fun ();
Else echo "function {$ fun} undefined ";
}

Function def (){
Echo "the default function def () is called because the parameter is not passed ()";
}
Function (){
Echo "function a is called! ";
}
Function B (){
Echo "function B is called! ";
}
?>


The instance code is as follows:

The code is as follows:

<? Php
Require_once showErrMsg. php;
$ _ Action = (isset ($ _ REQUEST [action])? $ _ REQUEST [action]: "");
If ($ _ action! = Null & $ _ action! = ){
If (function_exists ($ _ action )){
Eval ("$ _ action ();");
} Else {
Die (showErrMsg ("
Method does not exist in the current php file [". $ _ Action ."()]. "));
}
}
?>

<? Php
Function showErrMsg ($ strMsg ){
Return "". $ strMsg ."";
}
?>


On the front-end page, we can use different links to implement different functions. for example, we have such a link.

Http: // localhost/controller. php? Fun =

When the request arrives at controller. php, the PHP program will automatically execute function ().

Highlights:

On the page of this program, we first call the controller () function. This function first determines whether the function name ($ fun value) defined in the parameter is defined. if so, this function is called.

If fun is not defined in the $ _ GET parameter: http: // localhost/controller. php

Call a default function def ();

Is this code concise? You can copy the code back and check the effect. I will tell you that the code runs normally!

However, I am also unfortunate to tell you: in fact, there is a huge security risk in this neat code, which is a big security risk. what is it, if you are interested, you can refer to the relevant documents. I believe you will not immediately use this code on the server?

In addition, tests show that this method can not only dynamically call functions, but also dynamically instantiate objects, such:

The code is as follows:

$ Obj = new $ obj ();

The code is as follows:

The code is as follows:

<? Php
Class
{
Function foo ()
{
If (isset ($ this )){
Echo '$ this is defined (';
Echo get_class ($ this );
Echo ") n ";
} Else {
Echo "$ this is not defined. n ";
}
}
}

Class B
{
Function bar ()
{
A: foo ();
// Parent: foo ();
}
}

$ A = new ();
$ A-> foo (); // dynamic call, because the object is new
A: foo (); // static call, called directly using the class name, no new object
$ B = new B ();
$ B-> bar (); // in the $ B object, A: foo (); performs static calls.
B: bar ();
?>


Summary:Static and dynamic calls refer to the call to methods by classes and objects. dynamic calls refer to creating (new) objects and then using object variables to call methods; static is that no object is created and called directly using the class name. as for another object, it is very simple. different classes create different objects, such as class A and class B, $ a = new A (); $ B = new B (); $ a and $ B is another object.

I hope this article will help you with php programming.

Examples in this article describes the usage of dynamic calling in php. Share it with you for your reference. The specific analysis is as follows: in the program...

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.