php中的动态调用实例分析
这篇文章主要介绍了php中的动态调用的使用技巧,实例分析了动态调用的具体用法与注意事项,需要的朋友可以参考下
本文实例讲述了php中的动态调用具体用法。分享给大家供大家参考。具体分析如下:
在程序中如果加一大堆判断的确是一个很大的麻烦,例如这样:
复制代码 代码如下:
if($fun='a'){echo "哎呀!";}
elesif(){}
……
else{echo "嗯!";}
真的很麻烦并且造成程序后期阅读和修改时候的巨大麻烦,这时候我们可以把每一个要执行的代码段,用函数来实现,然后可以用一个更加NB的方法来实现这些功能,并且因为每一个函数实现一个功能,我们维护起来就简单多了.
进入正题,看看PHP动态调用函数到底有什么作用,在PHP中是可以动态调用函数的,像这样$fun(),PHP解析器可以根据变量$fun的值来调用对用的函数,,例如$fun='a',解析器看到的将是a();这样的形式,从而调用函数a,具体代码如下:
复制代码 代码如下:
//controller.php
(isset($_GET['fun'])&&$_GET['fun']!='')?$fun=$_GET['fun']:$fun='def';
controller($fun);
function controller($fun){
if(function_exists($fun)) $fun();
else echo "函数{$fun}未定义";
}
function def(){
echo "由于用户没有传递参数,调用了缺省的函数def()";
}
function a(){
echo "函数a被调用!";
}
function b(){
echo "函数b被调用!";
}
?>
实例代码如下:
复制代码 代码如下:
require_once showErrMsg.php;
$_action = (isset($_REQUEST[action])?$_REQUEST[action]:"");
if($_action!=null&&$_action!=){
if(function_exists($_action)){
eval("$_action();");
}else{
die(showErrMsg ( "
当前php文件中不存在方法[".$_action."()]。"));
}
}
?>
function showErrMsg($strMsg){
return "".$strMsg."";
}
?>
在前台页面我们可以用不同的链接来实现不同的功能,例如我们有这样一个链接
?fun=a
当请求到达controller.php的时候,PHP程序将会自动的执行函数a().
问题的重点:
在于我们在这个程序的页面首先调用了controller()函数。这个函数首先判断参数中定义的函数名称($fun的值)是否被定义,如果定义了就调用这个函数。
如果在$_GET参数中fun没有定义:
就调用一个缺省的函数def();
这样的代码是不是简洁很对呢?你可以把这些代码拷贝回去,自己看看效果——我肯定的告诉你,这些代码运行时正常的!
然而我也很不幸的告诉你:其实这段看起来整齐的代码有一个巨大的安全隐患在里面,很大,很大的安全隐患,具体是啥,感兴趣的朋友可以参考相关文档,相信你肯定不会把这一段代码立马用到服务器上的?
另外经过测试证实,这个方法不但可以动态调用函数,并且也可以动态实例化对象,像这样:
复制代码 代码如下:
$obj = new $obj();
代码如下:复制代码 代码如下:
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();//动态调用,因为new了对象
A::foo();//静态调用,直接用类名去调用,没有new对象
$b = new B();
$b->bar();//在对象$b中,A::foo();进行静态调用
B::bar();
?>
总结:静态、动态调用都指类、对象对其方法的调用,动态指的是创建(new)了对象,然后用对象变量去调用方法;静态则是没有创建对象,直接用类名去调用,至于另一个对象那就很简单了,不同的类创建不同的对象,比如class A;class B ,$a = new A();$b = new B();$a and $b 相对之间就是另一个对象了.
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
