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