不在对象上下文中使用$this 重新思考DB时间对象


Using $this when not in object context RethinkDB time object

我正在尝试转换一个包含rethinkdb datetime对象(由PHP-RQL库提供)的对象,但是我收到一个致命错误,说:

Using $this when not in object context

以下是代码的概述:

$day = new stdClass;
$datetime = new DateTime();
$arrival = r'time(
    $datetime->format('Y'),
    $datetime->format('m'),
    $datetime->format('d'),
    $datetime->format('H'),
    $datetime->format('i'),
    $datetime->format('s'),
    'Z'
);
$day->arrival = $arrival;
$day = object_to_array($day);

object_to_array()函数中,我得到了致命错误,其代码是:

function object_to_array($obj) {
    $array  = array(); // noisy $array does not exist
    $arrObj = is_object($obj) ? get_object_vars($obj) : $obj;
    foreach ($arrObj as $key => $val) {
        $val = (is_array($val) || is_object($val)) ? $this->getArray($val) : $val;
        $array[$key] = $val;
    }
    return $array;
}

我不记得我从哪里得到这个功能(它不是我的),但它过去对我很有用。

本质上,我的问题是为什么这个object_to_array函数会失败?

这是 r''time 函数返回的(一个对象): https://gist.github.com/fenfe1/6676924

注意:仅将时间对象转换为数组可以正常工作,但是传递包含时间的对象会失败。

最终,我需要得到一个数组以用于另一个函数,并且由于其他属性将添加到day对象中,因此将其保留为对象将是有益的。

错误消息

不在对象上下文中使用$this

已经告诉您函数失败的原因。你使用$this。$this通常在类内部用作对实例化对象的引用,但在您的情况下,您使用的是一个简单的函数,因此没有对象上下文。