2
votes

So, the question pretty much explains what i want. Here is the minimum code of what i am doing.

class AuthorizeController extends Controller
{
    private $aNetEnvironment;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->aNetEnvironment = env('ANetEnvironment');
    }

    public function setEnvironment()
    {
        $controller = new AnetController\GetCustomerProfileController($request);
        // $this->aNetEnvironment = SANDBOX
        $response = $controller->executeWithApiResponse( 
            \net\authorize\api\constants\ANetEnvironment::$this->aNetEnvironment 
        ); 
    }
}

Searching stackoverflow i got two options, have tried both with no luck.

Trying, {$this->aNetEnvironment} gives

syntax error, unexpected ')', expecting '('

Trying, $$this->aNetEnvironment gives

Object of class App\Http\Controllers\AuthorizeController could not be converted to string

Edit:

Trying, ${$this->aNetEnvironment} gives

Access to undeclared static property: net\authorize\api\constants\ANetEnvironment::$SANDBOX

Is there any other option ?

2
What is the content of env('ANetEnvironment');? - Kenny Horna
@HCK It's 'SANDBOX' - Shahbaz A.

2 Answers

3
votes

You could make use of the PHP's constant() helper. From the docs:

Signature:

constant ( string $name ) : mixed

Return the value of the constant indicated by name.

constant() is useful if you need to retrieve the value of a constant, but do not know its name. I.e. it is stored in a variable or returned by a function.

This function works also with class constants.

So in your case:

$response = $controller->executeWithApiResponse( 
    constant('\net\authorize\api\constants\ANetEnvironment::' . $this->aNetEnvironment) 
); 
0
votes

To use class properties as variable variables in this way you need to start with a $ and wrap the property in {} e.g. ${$this->property} so you should be able to use the following in your controller:

\net\authorize\api\constants\ANetEnvironment::${$this->aNetEnvironment}