Search code examples
phplaravelooplaravel-5authorize.net

How to use private property of controller as static property?


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 ?


Solution

  • 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) 
    );