vendredi 22 juillet 2016

How do I access a namespaced class constant in PHP using a variable for the class name?

I have the following.

my class
<?php
namespace app\models;    
use BingAds\CustomerManagement\CustomerManagementServiceSettings;

and

api class
namespace BingAds\CustomerManagement
{
    final class CustomerManagementServiceSettings
    {
        const SandboxEndpoint = 'http://ift.tt/2ai48dK';

How do I access the constant using a variable for the class name? It works for instantiating the class directly, but I can't access or instantiate the class when it is a variable.

new CustomerManagementServiceSettings();
$serviceSettingsClass = 'CustomerManagementServiceSettings';
$serviceSettingsClass::SandboxEndpoint; # line 80

Gives

PHP Fatal error: Class 'CustomerManagementServiceSettings' not found in /cygdrive/c/Users/Chloe/workspace/bestsales/models/BingAds.php on line 80

This question did not help.

In Java I could use

Class c = CustomerManagementServiceSettings.class
String se = (String) c.getField("SandboxEndpoint").get(null);





Aucun commentaire:

Enregistrer un commentaire