El uso de este $ cuando no esté en error contexto del objeto
-
28-09-2019 - |
Pregunta
Estoy recibiendo un error de PHP fatal al intentar ejecutar mi script:. Using $this when not in object context
Los recopila datos de secuencia de comandos de Google Analytics a través de la API, a continuación, lo muestra en una página
A continuación se muestra la pieza de código en las matrices de script:
$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 se establece por:
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;
}
Solución
Se debe escuchar el error. Está utilizando $this
cuando no esté en una clase.
Trate bien pueden suministrar el código de una clase u omitir $this->
. Uso global
lugar.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow