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.

hdsouza
  • 354
  • 4
  • 17
  • Is `imageapi` the name of a function or a class? – Nigel Ren Feb 06 '21 at 15:47
  • imageapi is an API Library – hdsouza Feb 06 '21 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?](https://stackoverflow.com/questions/534159/instantiate-a-class-from-a-variable-in-php) – KIKO Software Feb 06 '21 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. – Steven Feb 06 '21 at 16:30

1 Answers1

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;
Steven
  • 6,053
  • 2
  • 16
  • 28
  • 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 Feb 06 '21 at 17:18
  • @hdsouza That is exactly what this shows? – Steven Feb 06 '21 at 17:21
  • If you are suggesting $className = 'imagetyperzapi'; new $className; $bcs = new $className($ACCESS_TOKEN); It gives a fatal error on the first line – hdsouza Feb 06 '21 at 22:37
  • 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 Feb 06 '21 at 23:48
  • 1
    @hdsouza You need to take out the extra `new $className;` (the one which is on it's own) – Steven Feb 07 '21 at 00:01
  • That worked great. Thanks for your help and the many details. – hdsouza Feb 07 '21 at 04:06