0

I'm trying to create an Autoloader class, so that I'll be able to load all modules automatically. But the problem is, I want to set a global from configuration file, and later, just call all of them by using:

Autoloader::GetGlobals();

So far, I have these 3 files:

Configuration.php

<?php
global $Configuration;
$Configuration['Globals'] = "Core Database Templates Directory Debugger";
?>

Autoloader.Class.php

<?php
require_once('Configuration.php');
private static $Globals = "";
private static $MakeGlobal = "global ";

public static function GetGlobals()
    {
        $ParsedGlobals = "";
        $Globals2String = explode(" ", Autoloader::$Globals);
        foreach($Globals2String as $Global)
            $Globals[] = "$".$Global;
        $DefinedGlobals = count($Globals);
        for ($i = 0; $i < $DefinedGlobals; $i++) 
        {
            $LastElement = $DefinedGlobals - 1;
            if($i != $LastElement)
                $ParsedGlobals .= $Globals[$i].", ";
            else
                $ParsedGlobals .= $Globals[$i].";";
        }
        return Autoloader::$MakeGlobal.$ParsedGlobals;
    }
?>

I'm getting the right output:

global Core, Database, Templates, Directory, Debugger;

The next thing is that I want to interpret this as PHP code and not as a string, and I don't want to use eval() (because I've read many articles that says that this is the last function to be used).

So the question is, is it possible to run this string from return as PHP code by simply calling it as Autoloader::GetGlobals();?

10
  • What do you mean exactly, "as php code"? A string is a type of php code, so are you looking to convert that to an array? Commented Jun 9, 2014 at 17:08
  • I think he wants to inject it into a script and have it be interpreted by php not as a variable but as keywords Commented Jun 9, 2014 at 17:08
  • So i want to run it in php exactly as global variables, but not a simple string Commented Jun 9, 2014 at 17:10
  • 1
    Those articles telling you that eval is the last function to be used are trying to tell you its a bad idea to do what you're trying to do. Commented Jun 9, 2014 at 17:10
  • 1
    You do know that any variables that have already been defined in the global scope are available in the $GLOBALS super global array right? If you really needed global access to the variables, you could just use that. Commented Jun 9, 2014 at 17:20

1 Answer 1

1

It's almost as bad as using eval(), but there's variable variables, if you choose to go down this path of madness and chaos:

function foo($arg) {
   global $$arg; // note the $$
   echo "$arg / $$arg";
}

$a = 'bar';
foo('a');

output:

a / bar
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.