Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 854
shazyriver Avatar asked Jan 25 '26 08:01

shazyriver


1 Answers

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) 
); 
like image 56
Kenny Horna Avatar answered Jan 28 '26 01:01

Kenny Horna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!