首页 > 解决方案 > php 在调用中使用变量

问题描述

致命错误:访问未声明的静态属性:DTS\eBaySDK\Constants\GlobalIds::$stripped in /home/jimi13/public_html/dealpopup.com/ebayapi/finding/mine.php 在第 334 行

$stripped = str_replace(' ', '', $country);
$stripped = preg_replace('/\s+/', '', $stripped);
echo $stripped;

需要把它剥掉

$service = new Services\FindingService([
    'credentials' => $config['production']['credentials'],
    'globalId'    => Constants\GlobalIds::US
]);

需要把它放进去,但会抛出错误

$service = new Services\FindingService([
    'credentials' => $config['production']['credentials'],
    'globalId'    => Constants\GlobalIds::$stripped
]);

标签: phpapi

解决方案


这是一种可以使用命名空间访问类中的常量变量的方法。如果没有命名空间,那么您可以删除常量命名空间。

namespace Constants;

class Hello {
  const WORLD = 'Hello World';
}

$world = 'WORLD';
echo constant('Constants\Hello::' . $world);

推荐阅读