0

I need to specify a variable name for a function call (imageapi) and then execute the line.

//For example this is my working code:
require($Path.'/lib/imageapi.php');
new imageapi($ACCESS_TOKEN);

//I want to replace the above lines with:
require($Path.'/lib/imageapi.php');
$provider = 'imageapi';
new $provider($ACCESS_TOKEN);

Is anything like this doable? Any help is appreciated.

4
  • Is imageapi the name of a function or a class? Commented Feb 6, 2021 at 15:47
  • imageapi is an API Library Commented Feb 6, 2021 at 15:55
  • Have you actually tried the code you want? You may be surprised. Please note that if you use namespaces you must include them. See: instantiate a class from a variable in PHP? Commented Feb 6, 2021 at 16:15
  • @KIKOSoftware I don't think he would be surprised. It would look, for all intents and purposes, as though nothing happened. Because the object is lost in memory. Commented Feb 6, 2021 at 16:30

1 Answer 1

1

So, your code does work. Just not quite how you think it does (or maybe want/expect it to).

Using variables as class/function names

You can do this, pretty much as you have done. For example:

$getTotal = "array_sum";
$array    = [1,2,3,4,5,6,7];

echo $getTotal($array); // 28

You can do the same for a class

class test
{

    public static $count = 0;
    public $variable1 = "some var";

    public function __construct()
    {
        test::$count++;
    }

}

$className = 'test';

new $className;

echo test::$count; // 1

The problem with the above code is that you haven't assigned the class object to a variable and so it is lost to the abyss.

So you need to assign it to a variable:

$myClassInstance = new $className;

echo test::$count; // 2 :: because it's the second time we've called the class
                   //       and whether or not we keep the class in memory the
                   //       static variable is updated; because it is static!

This is helpful if you need to assign a class based off of some dynamic input... But in general terms it's best to stick to the class name!

$anotherClassInstance = new test;

echo test::$count; 3;
Sign up to request clarification or add additional context in comments.

6 Comments

I am not looking to create a new function. I already have one. All I am trying to do is replace the name with a variable.
@hdsouza That is exactly what this shows?
If you are suggesting $className = 'imagetyperzapi'; new $className; $bcs = new $className($ACCESS_TOKEN); It gives a fatal error on the first line
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function ImagetyperzAPI::__construct(), 0 passed in C:\temp\2.php on line 5 and at least 1 expected in C:\temp\lib\imagetyperzapi.php:81 . It fails on the Line "new $className;" .. Here is the full code: $ACCESS_TOKEN = "a12344567"; require('c:temp/lib/imagetyperzapi.php'); $className = 'imagetyperzapi'; new $className; $bcs = new $className($ACCESS_TOKEN);
@hdsouza You need to take out the extra new $className; (the one which is on it's own)
|

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.