Php-PHP Tutorial

Source: Internet
Author: User
Php Dynamic Call description. Dynamic calling is an advanced thing in php. today, let's take a look at what this advanced activity is. let's analyze and determine what this dynamic call is. if you need it, let's look at it. Dynamic calling is an advanced thing in php. today, let's take a look at what this advanced activity is. let's analyze and determine what this dynamic call is. if you need it, let's look at it.

Adding a lot of judgments in the program is indeed a great deal of trouble! For example:

The code is as follows:
If ($ fun = 'a') {echo "Oh !";}
Elesif (){}
......
Else {echo "Well !";};

It is really troublesome and it will cause a great deal of trouble in later reading and modifying the program!

At this time, we can use functions to implement every code segment to be executed.
Then you can use a more NB method to implement these functions.
And because each function implements a function, it is much easier to maintain.

Go to the topic and see what the function of PHP dynamic call can do:
In PHP, functions can be dynamically called. for example, $ 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 the form a (); to call function. The code is as follows:

The code is as follows:
// Program Source: PHP iask http://www.bkjia.com
// 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! ";
}
?>

Instance

The code is as follows:

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 ."()]. "));
}
}
?>

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 and big security risk! Specifically, I will leave "security questions about PHP dynamic function call" to be launched on time at tomorrow. you certainly won't use this code on the server immediately?

In addition, tests show that this method can not only dynamically call functions, but also dynamically instantiate objects, such as: $ obj = new $ obj ();

The code is as follows:

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: both static and dynamic calls refer to the calls of classes and objects to their methods. dynamic calls refer to creating (new) objects and then using object variables to call methods; if it is static, 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; class B, $ a = new A (); $ B = new B (); $ a and $ B is another object.


....

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.