Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP在不修改现有类的情况下从类中获取值_Php - Fatal编程技术网

PHP在不修改现有类的情况下从类中获取值

PHP在不修改现有类的情况下从类中获取值,php,Php,我正在使用以下工具: 类文件包括以下行: * 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',

我正在使用以下工具:

类文件包括以下行:

 * 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'=>数组('method'=>'GET','uri'=>'/products/%s/book'),
,就像它通过
$getOrderbook=$exchange->getOrderbook()调用它一样。请在后一行中包含“level=2”的位置?

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


由于属性
$endpoints
是公共的,您可以从类外访问它(请参见下文):


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

$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

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

$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

您正在替换所有端点,并且调用其他方法可能会导致错误。我刚刚尝试说明op可以在类外更新
$endpoint
(我认为这不会导致错误)。然而,op只能更新uri部分,正如您在回答中所示,我认为我可以做一些类似于以下内容的事情:
$getOrderbook=$exchange->getOrderbook([
level=2)`但它不起作用不起作用,因为它不应该将端点作为参数。您正在替换所有端点并调用其他方法可能会导致错误。我刚刚试图证明op可以在类外更新
$endpoint
(我认为这不会导致错误)但是,op只能更新uri部分,正如您在回答中所示,我想我可以做类似的事情,
$getOrderbook=$exchange->getOrderbook([
level=2);`但它不起作用不起作用,因为它不应该将端点作为参数
$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