Dynamically generate classes at runtime in php?

后端 未结 10 1055
一向
一向 2020-12-09 15:06

Here\'s what I want to do:

$clsName = substr(md5(rand()),0,10); //generate a random name
$cls = new $clsName(); //create a new instance

function __autoload(         


        
10条回答
  •  被撕碎了的回忆
    2020-12-09 15:35

    its funny, actually this is one of the few things where eval doesnt seem such a bad idea.

    as long as you can ensure that no user input will ever enter the eval.

    you still have downsides like when your using a bytecode cache that code wont be cached etc etc. but the security issues of eval are pretty much related to having user inputy in the eval, or to ending up in the wrong scope.

    if you know what you are doing, eval will help you with this.

    That said, in my opinion you are much better off when you no rely on type-hinting for your validation, but you have one function

    DoSomething_Validate($id)
    { 
       // get_class($id) and other validation foo here
    }
    

提交回复
热议问题