PHP从类中获取值而不修改现有类

时间:2016-05-13 10:36:29

标签: php

我正在使用以下Coinbase PHP api

类文件包含以下行:

 * An array of API endpoints
 */
 public $endpoints = array(
 'book' => array('method' => 'GET', 'uri' => '/products/%s/book'),
 );

 public function getOrderBook($product = 'BTC-EUR') {
 //$this->validate('product', $product);
 return $this->request('book', array('id' => $product));
 }

在我的档案中,我用它来称呼:

$exchange = new CoinbaseExchange();//Connect to Coinbase API
$getOrderbook = $exchange->getOrderBook();
print_r($getOrderbook);

什么都没有。

虽然如果我修改了这个类:

'book' => array('method' => 'GET', 'uri' => '/products/%s/book'),

要:

'book' => array('method' => 'GET', 'uri' => '/products/%s/book?level=2'),

我在文件中得到了所需的输出。

如何通过'book' => array('method' => 'GET', 'uri' => '/products/%s/book'),将课程保留为$getOrderbook = $exchange->getOrderBook();。我在哪里包括'level = 2`在后一行?

2 个答案:

答案 0 :(得分:2)

由于属性$endpoints是公开的,您可以从课堂外访问它(见下文):

$exchange = new CoinbaseExchange();//Connect to Coinbase API
$exchange->endpoints = array('book' => array('method' => 'GET', 'uri' => '/products/%s/book?level=2'))
$getOrderbook = $exchange->getOrderBook();
print_r($getOrderbook);

答案 1 :(得分:0)

您可以对端点进行临时更改:

$oldEndpoint= $exchange->endpoints['book']; // save previous value
$exchange->endpoints['book']['uri'] .= '?level=2'; // make needed changes
$exchange->getOrderBook();
$exchange->endpoints['book'] = $oldEndpoint; // reset to old value