Dynamic invocation instance analysis in PHP, PHP invoke instance Analysis _php tutorial

Source: Internet
Author: User
Tags what php

Dynamic invocation instance analysis in PHP, PHP invocation Instance analysis


The examples in this article describe the use of dynamic invocation in PHP. Share to everyone for your reference. The specific analysis is as follows:

Adding a lot of judgment in a program is really a big hassle, for example:
Copy the code as follows: if ($fun = ' a ') {echo "Oops! ";}
Elesif () {}
......
Else{echo "Well! ";}
Really troublesome and caused by the program late reading and modification of the big * annoying, this time we can put each piece of code to execute, with a function to implement, and then we can use a more NB method to implement these functions, and because each function to implement a function, we maintain a lot easier.

To get to the point, see what PHP dynamic Call function, in PHP can be dynamically called functions, such as $fun (), PHP parser can be based on the value of the variable $fun to invoke the function, such as $fun= ' a ', the parser will see a (); Thus calling function A, the code is as follows:
Copy CodeThe 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} not defined";
}

function Def () {
echo "The default function Def () is called because the user has not passed the parameter."
}
function A () {
echo "function A is called!" ";
}
Function B () {
echo "function B is called!" ";
}
?>
The instance code is as follows:
Copy CodeThe 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 ("
There is no method in the current PHP file [". $_action." ()]。 "));
}
}
?>

<?php
function Showerrmsg ($STRMSG) {
Return "". $strMsg. "";
}
?>
In the front page we can use different links to achieve different functions, for example, we have such a link

Http://localhost/controller.php?fun=a

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

The focus of the issue:

Is that we first called the controller () function on the page of the program. This function first determines whether the function name ($fun value) defined in the parameter is defined and is called if defined.

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

A default function, Def (), is called;

Is this code concise and right? You can copy the code back and look at the results yourself-I'm sure to tell you that the code runs normally!

However, I am also very unfortunate to tell you: In fact, this looks neat code has a huge security hidden in the inside, very large, very large security risks, specifically what, interested friends can refer to the relevant documents, I believe you will not put this piece of code immediately used on the server?

In addition, the test proves that this method can not only invoke the function dynamically, but also instantiate the object dynamically, like this: copy the Code code as follows: $obj = new $obj (); Code is as follows:
Copy the Code code as follows: <?php
Class A
{
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 ();
$a->foo ();//Dynamic call, because new object
A::foo ();//static call, directly with the class name to call, no new object
$b = new B ();
$b->bar ();//In the object $b, A::foo (); Make a static call
B::bar ();
?>
Summary: static, dynamic invocation refers to a class, the object of its method calls, dynamic refers to the creation (new) object, and then use object variables to invoke the method, static is not created object, directly with the class name to call, as to another object that is very simple, different classes to create different objects, For example, class A;class B, $a = new A (), $b = new B (), $a and $b opposite is another object.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/939411.html www.bkjia.com true http://www.bkjia.com/PHPjc/939411.html techarticle Dynamic Invocation instance analysis in PHP, PHP invocation Instance Analysis This article describes the dynamic invocation in PHP concrete usage. Share to everyone 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.