不在对象上下文中时使用 $this 错误
我在尝试执行脚本时遇到致命的 PHP 错误:不在对象上下文中使用 $this
该脚本通过 API 从 Google Analytics 收集数据,然后将其显示在页面上。
下面是脚本终止的代码片段:
$records = $this->getAnalyticRecords ( date ( 'Y-m-d', $startTime ), date ( 'Y-m-d', $endEnd ), 'ga:date', 'ga:visitors,ga:newVisits,ga:visits,ga:pageviews,ga:timeOnPage,ga:bounces,ga:entrances,ga:exits' );
getAnalyticsRecords 的设置方式为:
function getAnalyticRecords($startDate, $endDate, $dimensions, $metrics, $sort = '', $maxResults = '') {
$url = 'https://www.google.com/analytics/feeds/data';
$url .= "?ids=" . $this->profile;
$url .= "&start-date=" . $startDate;
$url .= "&end-date=" . $endDate;
$url .= "&dimensions=" . $dimensions;
$url .= "&metrics=" . $metrics;
if (! empty ( $sort )) {
$url .= "&sort=" . $sort;
}
if (! empty ( $maxResults )) {
$url .= "&max-results=" . $maxResults;
}
if (($feedData = $this->fetchFeed ( $url )) === FALSE) {
return array ();
}
$doc = new DOMDocument ( );
$doc->loadXML ( $feedData );
$results = array ();
$aggregates = $doc->getElementsByTagName ( 'aggregates' );
foreach ( $aggregates as $aggregate ) {
$metrics = $aggregate->getElementsByTagName ( 'metric' );
foreach ( $metrics as $metric ) {
$results ['aggregates'] ['metric'] [$metric->getAttribute ( 'name' )] = $metric->getAttribute ( 'value' );
}
}
$entries = $doc->getElementsByTagName ( 'entry' );
foreach ( $entries as $entry ) {
$record = array ();
$record ["title"] = $entry->getElementsByTagName ( 'title' )->item ( 0 )->nodeValue;
$dimensions = $entry->getElementsByTagName ( 'dimension' );
foreach ( $dimensions as $dimension ) {
$record ['dimension'] [$dimension->getAttribute ( 'name' )] = $dimension->getAttribute ( 'value' );
}
$metrics = $entry->getElementsByTagName ( 'metric' );
foreach ( $metrics as $metric ) {
$record ['metric'] [$metric->getAttribute ( 'name' )] = $metric->getAttribute ( 'value' );
}
$results ['entry'] [] = $record;
}
return $results;
}
I'm getting a fatal PHP error when trying to execute my script: Using $this when not in object context
The script collects data from Google Analytics via API, then displays it on a page.
Below is the piece of code where the script dies:
$records = $this->getAnalyticRecords ( date ( 'Y-m-d', $startTime ), date ( 'Y-m-d', $endEnd ), 'ga:date', 'ga:visitors,ga:newVisits,ga:visits,ga:pageviews,ga:timeOnPage,ga:bounces,ga:entrances,ga:exits' );
getAnalyticsRecords is set by:
function getAnalyticRecords($startDate, $endDate, $dimensions, $metrics, $sort = '', $maxResults = '') {
$url = 'https://www.google.com/analytics/feeds/data';
$url .= "?ids=" . $this->profile;
$url .= "&start-date=" . $startDate;
$url .= "&end-date=" . $endDate;
$url .= "&dimensions=" . $dimensions;
$url .= "&metrics=" . $metrics;
if (! empty ( $sort )) {
$url .= "&sort=" . $sort;
}
if (! empty ( $maxResults )) {
$url .= "&max-results=" . $maxResults;
}
if (($feedData = $this->fetchFeed ( $url )) === FALSE) {
return array ();
}
$doc = new DOMDocument ( );
$doc->loadXML ( $feedData );
$results = array ();
$aggregates = $doc->getElementsByTagName ( 'aggregates' );
foreach ( $aggregates as $aggregate ) {
$metrics = $aggregate->getElementsByTagName ( 'metric' );
foreach ( $metrics as $metric ) {
$results ['aggregates'] ['metric'] [$metric->getAttribute ( 'name' )] = $metric->getAttribute ( 'value' );
}
}
$entries = $doc->getElementsByTagName ( 'entry' );
foreach ( $entries as $entry ) {
$record = array ();
$record ["title"] = $entry->getElementsByTagName ( 'title' )->item ( 0 )->nodeValue;
$dimensions = $entry->getElementsByTagName ( 'dimension' );
foreach ( $dimensions as $dimension ) {
$record ['dimension'] [$dimension->getAttribute ( 'name' )] = $dimension->getAttribute ( 'value' );
}
$metrics = $entry->getElementsByTagName ( 'metric' );
foreach ( $metrics as $metric ) {
$record ['metric'] [$metric->getAttribute ( 'name' )] = $metric->getAttribute ( 'value' );
}
$results ['entry'] [] = $record;
}
return $results;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该听一下错误。您不在课堂上时使用
$this
。尝试将代码包含在类中或省略
$this->
。请改用global
。You should listen to the error. You are using
$this
when not in a class.Try either enclosing your code in a class or omitting
$this->
. Useglobal
instead.