我不确定为什么,但我收到此错误
Fatal error: Call to a member function update() on a non-object in /home/XXXXXXXXXXXXXXXXX/classes/core.php on line 22
在那个页面上,我有这个
public function updatemongo($from,$data)
{
$this->db = $m->exchange_rates;
$collection = $this->db->exchangeDB;
$collection->update(array("from" => $from), $data);
}
这就是我调用此函数的方式
foreach ($currencies as $to)
{
if($from != $to)
{
$url = 'http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s='.$from.$to.'=X';
$handle = fopen($url, 'r');
if ($handle) {
$result = fgetcsv($handle);
fclose($handle);
}
$newdata = array('$set' => array("exchangehistory.{$result[1]}.{$result[2]}" => array("to" => $to, "rate" => $result[0], "updated" => $result[2])));
$fetch->updatemongo($from,$newdata);
$newdata = array('$set' => array("currentexchange" => array("to" => $to, "rate" => $result[0], "updated" => $result[2])));
$fetch->updatemongo($from,$newdata);
}
}
是的,需要访问此文件的文件也具有require_once("core.php");
请让我知道为什么这不起作用。
updatemongo()
函数无权访问$m
变量。请像这样将其传递给函数:
$fetch->updatemongo($m, $from, $newdata);
并将函数定义更改为:
公共函数更新Mongo($m,$from,$data) {
或者,可以在创建连接后将对象的 m
属性设置为连接。例如:
public function __construct)
{
$this->m = new Mongo();
}
...
public function updatemongo($from, $data)
{
$this->db = $this->m->exchange_rates;
$collection = $this->db->exchangeDB;
$collection->update(array("from" => $from), $data);
}
或者,也许您可以使用上面已经$this->exchange_rates
...在任何情况下,您都不会使$m
可用于该函数。
看起来像一个错别字:对象"$m"没有在函数updatemongo()
中实例化
要么在此处创建它,要么通过global $m;
(如果存在)获得访问权限。
$this->db->exchangeDB 在放入 $collection 时包含 null 或类似内容?这对我们来说很难说,因为我们不知道该变量在哪里实例化。
$collection->update(array("from" => $from), $data);
是错误的原因,错误很明显:$update不是对象。 否则它会抱怨"PHP 致命错误:调用未定义的方法 YourClass::yourMethod() 在/my/php/file.php 第 xx 行"