swl1020
swl1020

Reputation: 823

Call script based on variable name

I have a parent function that is passed a variable called $scriptName. Depending on what is stored in $scriptName, I want to call the corresponding script.

I have a file called childOneScript.php

If $scriptName=="childOne", how do I call childOneScript.php?

Upvotes: 0

Views: 94

Answers (4)

robbmj
robbmj

Reputation: 16526

You can just use the normal require

require_once $scriptName . 'Script.php';

Keep in mind however that if the script does not exist PHP will raise a fatal error, so you should be checking that the script does indeed exist.

/**
   Assumes that $name does not contain the PHP extension and
   this function works with relative paths.
   If the file does not exist, returns false, true otherwise
*/
function loadScript($name) {
    $name = $name . '.php';
    if (!file_exists($name) || !is_readable($name)) {
        // raise an error, throw an exception, return false, it's up to you
        return false;
    }
    require_once $name;
    return true;
}
$loaded = loadScript('childOneScript');

Alternatively you can use include, PHP will only raise a warning if it can't find the script.

There are a few security concerns with the above function. For example if the user is allowed to define the value of $scriptName an attacker could use it to read any file that is readable to the web server user.

Here is an alternative that limits the number of files that can be dynamically loaded to just the files that need be loaded in this manner.

class ScriptLoader {

    private static $whiteList = array(
        // these files must exist and be readable and should only include
        // the files that need to be loaded dynamically by your application
        '/path/to/script1.php' => 1,
        '/path/to/script2.php' => 1,
    );

    private function __construct() {}

    public static function load($name) {
        $name = $name . '.php';
        if (isset(self::$whiteList[$name])) {
            require_once $name;
            return true;
        }
        // the file is not allowed to be loaded dynamically
        return false;
    }
}

// You can call the static method like so.
ScriptLoader::load('/path/to/script1');     // returns true
ScriptLoader::load('/path/to/script2');     // returns true 
ScriptLoader::load('/some/other/phpfile');  // returns false

Upvotes: 2

Hugo Delsing
Hugo Delsing

Reputation: 14173

You could use the include or require methods in PHP

<?php
function loadScript($scriptName) {
  if ($scriptName=="childOne") {
    include("childOneScript.php");
  }
}
?>

Keep in mind though that the included script is included where you load it. So it's inside the loadScript function. That means you cannot access it's content outside its scope.

Upvotes: 1

Rahul Desai
Rahul Desai

Reputation: 15501

You can simply do:

if ($scriptName=="childOne"){
    require_once(childOneScript.php);
}

The require_once statement will check if the file has already been included, and if so, not include (require) it again.

Readup: require_once() | PHP

Upvotes: 2

Whirl Mind
Whirl Mind

Reputation: 884

Just use the include statement inside the If condition.

if $scriptName == "childOne" {
     include childOneScript.php;
   }

Upvotes: 1

Related Questions