如何安全和动态地创建现有PHP类的实例? [英] How to safely and dynamically create an instance of an existing PHP class?

查看:130
本文介绍了如何安全和动态地创建现有PHP类的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个文件Foo.php:

 <?php 
接口ICommand
{
function doSomething();


class Foo实现ICommand
{
public function doSomething()
{
returnI'm Foo output;
}
}

?>

如果我想创建一个类型为Foo的类,我可以使用:

  require_once(path / to / Foo.php); 
$ bar = new Foo();

但是说我创建了一个链命令模式,我有一个配置文件,用于注册所有可能的类,并基于字符串创建这些类的实例它们存在于配置文件中。

 注册(Foo,path / to / Foo.php); 

函数寄存器($ className,$ classPath)
{
require_once($ classPath); //如果文件不包含错误,但是让
//假定文件Foo.php存在。
$ classInstance = new $ className; //如果Foo类不是
//在文件Foo.php中定义,那么会发生什么?

$ classInstance-> doSomething(); //如果这个代码在
//全部执行,这里会发生什么?
// etc ...
}

如何确保这些类实际上是配置文件所在的地方?如果类不存在(但是文件是)会发生什么,它会创建一个动态生成的类的实例,没有进一步的描述?

解决方案

您可以使用 class_exists 检查是否已经定义了一个类。



如果您正在动态调用类,并且从同一函数中调用该类的方法,您也可以调用该方法动态(除非您的所有类都具有完全相同的方法,如果是这样,您还可以使用 method_exists



最后还可以使用 file_exists 以确保可以包含该文件:

  register(Foo,path / to / Foo.php,bar,array 'arg1','arg2')); 

函数寄存器($ className,$ classPath,$ methodName,$ args)
{
if(!file_exists($ classPath))return false;

require_once($ classPath);

if(!class_exists($ className))return false;

$ classInstance = new $ className;

if(!method_exists($ classInstance,$ methodName))return false;

$ classInstance-> $ methodName($ args);
}


Say I have a file Foo.php:

<?php
interface ICommand
{
     function doSomething();
}

class Foo implements ICommand
{
    public function doSomething()
    {
        return "I'm Foo output";
    }
}

?>

If I want to create a class of type Foo I could use:

require_once("path/to/Foo.php") ;
$bar = new Foo(); 

But say that I've created a Chain of Command Pattern and I have a configuration file that registers all the possible classes and creates an instance of these classes based on strings that are present in the configuration file.

register("Foo", "path/to/Foo.php");

function register($className, $classPath)
{
    require_once($classPath); //Error if the file isn't included, but lets 
                              //assume that the file "Foo.php" exists.
    $classInstance = new $className; //What happens here if the class Foo isn't 
                                     //defined in the file "Foo.php"? 

    $classInstance->doSomething(); //And what happens here if this code is executed at
                                   //all?
    //Etc...
}

How do I make sure that these classes are actually where the configuration file says they are? And what happens if a class isn't there (but the file is), will it create an instance of a dynamically generated class, that has no further description?

解决方案

You can use class_exists to check if a class has been defined.

If you are calling a class dynamically and also calling a method on that class from within the same function, you may also wish to call that method dynamically (unless all of your classes have the exact same method. If that is the case, you can also use method_exists

Finally, you can also use file_exists to ensure the file can be included:

register("Foo", "path/to/Foo.php", "bar", array('arg1', 'arg2'));

function register($className, $classPath, $methodName, $args)
{
    if(!file_exists($classPath)) return false;

    require_once($classPath);

    if(!class_exists($className)) return false;

    $classInstance = new $className;

    if(!method_exists($classInstance, $methodName)) return false;

    $classInstance->$methodName($args);
}

这篇关于如何安全和动态地创建现有PHP类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆