phplaravelstaticpublicvendor

Set Public Static in Vendor Folder


I want to set public static variable inside vendor folder.

I want to change this

public static $serverKey_as = 'my-secret-key';

into this, get the key from config -> app.php file

public static $serverKey_as = config('app.serverkey_as');

But i get this error

Symfony\Component\ErrorHandler\Error\FatalError: Constant expression contains invalid operations in file

this is my code in config -> app.php

'serverkey_as' => env('SERVERKEY_AS', 'my-defauly-secret-key'),

and this is my .env

SERVERKEY_AS = 'my-secret-key'

and this is what i've try but still no luck

<?php

namespace Midtrans;

class Config
{
    public static  $serverKey_as;
    public function __construct()
    {
        return self::$serverKey_as = config('app.serverkey_as');
    }
}

Got any hint for me?


Solution

  • First of all: you really shouldn't change code in the /vendor directory. That directory contains code made and maintained by other people.

    Furthermore, an initial static variable assignment cannot contain function calls of any kind. I would suggest using the boot() method in your AppServiceProvider to change the static variable to the value you want:

    public function boot()
    {
        \Midtrans\Config::$serverKey_as = config('app.serverkey_as');
        //...
    }