I'm trying to make IntegratedApp class constant use value from the config file and then use the constant in MakeConnectionController. But the constant PORTAL_BASE_URL_STAG, inside the IntegratedApp class, when assigning a static method to it, an underline error (error message: Constant expression contains invalid operations) appeared on the constant. Is there a way to assign variables to the constant class without using the static method?
Code Example:
config file :
[test]
stagingApiUrl = 'https://api-test-first.com/';
IntegratedApp.php file :
namespace Project\Constants;
class IntegratedApp
{
const PORTAL_BASE_URL_STAG = self::getUrl();
public static function getUrl() {
$base_url = $this->config->test->stagingApiUrl ? $this->config->test->stagingApiUrl : 'https://api-test-second.com/';
return (string)$base_url;
}
}
MakeConnectionController.php file:
use Project\Constants\IntegratedApp;
class MakeConnectionController extends BaseController
{
$baseUrl = IntegratedApp::PORTAL_BASE_URL_STAG;
}
You can do it with this workaround ( use define() ):
<?php
namespace Project\Constants;
define('PORTAL_BASE_URL_STAG',IntegratedApp::getUrl());
class IntegratedApp
{
const PORTAL_BASE_URL_STAG = PORTAL_BASE_URL_STAG;
public static function getUrl() {
return (string)"Foo";
}
}
print IntegratedApp::PORTAL_BASE_URL_STAG;//Foo
Hopfully, you have not so mutch magic going on.
And you should fix the $this
usage, because in static methods you dont have an access to $this
.
Don't ask why this works :-) It works. Seems like there is a kind of late class constant binding going on here.