Who would have ever thought eval would be good for anything?

Place to place any code snippets, completed games, or even uncompleted games for IR users to use.
Post Reply
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Who would have ever thought eval would be good for anything?

Post by Chris »

Making a class name from records from a database:

Code: Select all

    /**
     * Generates the name for a Dynamic class to be extended from this class
     * @param Form $formModel
     * @param Language $languageModel
     * @param AppVersion $appVersionModel
     * @return string
     */
    public static function generateSafeDynamicClassNameString($formModel, $languageModel, $appVersionModel) {
        return preg_replace('/[^a-zA-Z0-9\']/', '_', $formModel->form_name) .
                preg_replace('/[^a-zA-Z0-9\']/', '_', $languageModel->language_name) .
                preg_replace('/[^a-zA-Z0-9\']/', '_', $appVersionModel->version_name);
    }

    /**
     * Creates a new class based on the form name, language name and app version version name
     * This class is extended from DynamicFormModel
     * @param Form $formModel
     * @param Language $languageModel
     * @param AppVersion $appVersionModel
     * @return string the name of the newley created class
     */
    public static function createDynamicClass($formModel, $languageModel, $appVersionModel) {
        $dynamicClassName = self::generateSafeDynamicClassNameString($formModel, $languageModel, $appVersionModel);
        eval('class ' . $dynamicClassName . ' extends DynamicFormModel {}');
        return $dynamicClassName;
    }

    /**
     * Creates a new class based on the form name, language name and app version version name
     * This class is extended from DynamicFormModel
     * @param Form $formModel
     * @param Language $languageModel
     * @param AppVersion $appVersionModel
     * @return *DynamicallyNamedClass* which extends from DynamicFromModel
     */
    public static function instantiateDynamicObject($formModel, $languageModel, $appVersionModel) {
        if (!$formModel instanceof Form || !$languageModel instanceof Language || !$appVersionModel instanceof AppVersion)
            throw new ErrorException('Invalid parameter given');

        $dynamicClassName = self::createDynamicClass($formModel, $languageModel, $appVersionModel);
        return new $dynamicClassName($formModel, $appVersionModel, $languageModel);
    }
 
The magical controller:

Code: Select all

$appVersionModel = AppVersion::model()->findByPk((int) $_POST['appVersionId']);
$formModel = Form::model()->findByPk((int) $_POST['formId']);
$languageModel = Language::model()->findByPk((int) $_POST['languageId']);

$dynamicFormModel = DynamicFormModel::instantiateDynamicObject($formModel, $languageModel, $appVersionModel);
$dynamicClassName = DynamicFormModel::generateSafeDynamicClassNameString($formModel, $languageModel, $appVersionModel);
            
foreach ($_POST[$dynamicClassName] as $attribute => $value) {
     $dynamicFormModel->$attribute = $value;
} 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Who would have ever thought eval would be good for anyth

Post by a_bertrand »

I actually use it for the NWE engine, to be able to have dynamic (based on DB entries) logic which let game owners add logic without modifying the actual game code.
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Who would have ever thought eval would be good for anyth

Post by hallsofvallhalla »

yep i use eval quite a bit too. Love it for those real dynamic variable names and building crazy wacky stuff.
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Who would have ever thought eval would be good for anyth

Post by Jackolantern »

I think the reason why eval-type functions get such a bad rap is because people don't respect it and put user input directly into it. That is obviously a problem, particularly on the server with PHP. But with the proper care, and keeping user input safely a bit back from it, it can be quite useful. :cool:
The indelible lord of tl;dr
Post Reply

Return to “Code Sharing”