-1

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 = 'https://clientcenter.api.sandbox.bingads.microsoft.com/Api/CustomerManagement/v9/CustomerManagementService.svc';

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);
Community
  • 1
  • 1
Chloe
  • 25,162
  • 40
  • 190
  • 357
  • @Nitin `$$serviceSettingsClass::SandboxEndpoint;` is a syntax error. – Chloe Jul 22 '16 at 23:55
  • Instead try this on `line 79`: `$serviceSettingsClass = new CustomerManagementServiceSettings();` – ŽaMan Jul 22 '16 at 23:59
  • 1
    You need to give the full path of the class: $var = '\\BingAds\\CustomerManagement\\CustomerManagementServiceSettings'; $var::SandboxEndpoint – Nitin Jul 23 '16 at 00:01

1 Answers1

1

Observe what akhoondi at php.net pointed here:

One must note that when using a dynamic class name [...] the "current namespace" [...] is global namespace.

So to make that work you will need to specify the full namespace when using a dynamic class name.

Tiago
  • 732
  • 1
  • 7
  • 10
  • Shoot. The different classes have different namespaces. Now when the API version changes I'll have to go through the whole code to find each fully qualified class name rather than having them all together at the top of the file. – Chloe Jul 23 '16 at 01:39
  • @Chloe you could create a map of the namespaces, which i use to check for structural mistakes and unittests. In this case you could call the right namespace group in different class, like a class loader. – Nitin Jul 23 '16 at 12:46