是否可以以简化的方式多次实例化同一个类,而不是我拥有的?
include ("php/functions.php");
date_default_timezone_set('Europe/Lisbon');
require 'php/gapi.class.php';
$ga1 = new gapi('XXXX@gmail.com','XXXX');
$ga2 = new gapi('XXXX@gmail.com','XXXX');
$ga3 = new gapi('XXXX@gmail.com','XXXX');
$ga1->requestReportData( $report_id = XXXX, $dimension = array('browser'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visitors', $filter = 'browser == Firefox || browser == Chrome || browser == Internet Explorer || browser == Safari || browser == Opera && visits > 10', $start_date = '2012-10-17' );
$ga2->requestReportData( $report_id = XXXX, $dimension = array('country'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visits', $filter = 'country != (not set) && visits > 3', $start_date = '2012-10-17', null, null, 10 );
$date2 = date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-15, date("Y")));
$today = date('Y-m-d');
$ga3->requestReportData($report_id = XXXX, $dimension = array('date'), $metrics = array('pageviews','visits'), $sort_metric = array('date') , $filter = '',$date2,$today, null,15);
PS:此代码按预期工作......我只是想知道是否有更有效的方法来实现这一目标......
编辑:要回答一些关于所提供的解决方案的问题,这里是一个应用程序的例子,几百行后来在同一个文件中:
<h2 class="verde">By country (10 relvant results)</h2>
[useless code ommited...]
<tbody>
<?php
$tp = 0;
foreach($ga2->getResults() as $r2){
if($tp==10) break;
?>
<tr>
<td><?php echo $r2 ?></td>
<td><?php echo $r2->getVisitors() ?></td>
<td><?php echo $r2->getVisits() ?></td>
<td><?php echo $r2->getPageviews() ?></td>
</tr>
<?php
$tp++;
}
?>
<tr style="font-weight:700">
<td><strong>TOTAIS</strong></td>
<td><strong><?php echo $ga2->getVisitors() ?></strong></td>
<td><strong><?php echo $ga2->getVisits() ?></strong></td>
<td><strong><?php echo $ga2->getPageviews() ?></strong></td>
</tr>
</tbody>
我不确定我是否可以在此处发布链接...但此代码可以在http://jb.utad.pt/gapi/
上看到答案 0 :(得分:3)
您只需要one object
即可访问某个功能。您可以根据需要更改传递给函数的参数。
拥有相同类的multiple objects
并不是有效的代码,因为每个对象占用了更多的额外内存。
因此,你只需要,
$ga = new gapi('XXXX@gmail.com','XXXX');
$ga->requestReportData( $report_id = XXXX, $dimension = array('browser'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visitors', $filter = 'browser == Firefox || browser == Chrome || browser == Internet Explorer || browser == Safari || browser == Opera && visits > 10', $start_date = '2012-10-17' );
$ga->requestReportData( $report_id = XXXX, $dimension = array('country'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visits', $filter = 'country != (not set) && visits > 3', $start_date = '2012-10-17', null, null, 10 );
$date2 = date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-15, date("Y")));
$today = date('Y-m-d');
$ga->requestReportData($report_id = XXXX, $dimension = array('date'), $metrics = array('pageviews','visits'), $sort_metric = array('date') , $filter = '',$date2,$today, null,15);
答案 1 :(得分:0)
我假设您的电子邮件地址相同,因此您只需要一个对象。
include ("php/functions.php");
date_default_timezone_set('Europe/Lisbon');
require 'php/gapi.class.php';
$ga1 = new gapi('XXXX@gmail.com','XXXX');
$ga1->requestReportData( $report_id = XXXX, $dimension = array('browser'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visitors', $filter = 'browser == Firefox || browser == Chrome || browser == Internet Explorer || browser == Safari || browser == Opera && visits > 10', $start_date = '2012-10-17' );
$ga1->requestReportData( $report_id = XXXX, $dimension = array('country'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visits', $filter = 'country != (not set) && visits > 3', $start_date = '2012-10-17', null, null, 10 );
$date2 = date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-15, date("Y")));
$today = date('Y-m-d');
$ga1->requestReportData($report_id = XXXX, $dimension = array('date'), $metrics = array('pageviews','visits'), $sort_metric = array('date') , $filter = '',$date2,$today, null,15);
不需要多次实例化同一个对象。
答案 2 :(得分:0)
好吧,可能会有所帮助:
$ga1 = new gapi('XXXX@gmail.com','XXXX');
$ga2 = clone $ga1;
答案 3 :(得分:0)
最大的问题是:多次实例化gapi类是否有意义?它是否是您尝试解决的“问题”的有效解决方案?在大多数情况下,您可以将相同的对象实例化数千次,并且不会产生太大的差异(内存方面)。告诉您将所有对象合并到一个实例中的人完全忽略了OOP的观点。如果有同一个类的多个实例是有意义的,请不要为了微优化而更改该类。
答案 4 :(得分:0)
在您的代码示例中,实际上这些调用是多余的:
$ga1->requestReportData( $report_id = XXXX, $dimension = array('browser'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visitors', $filter = 'browser == Firefox || browser == Chrome || browser == Internet Explorer || browser == Safari || browser == Opera && visits > 10', $start_date = '2012-10-17' );
$ga2->requestReportData( $report_id = XXXX, $dimension = array('country'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visits', $filter = 'country != (not set) && visits > 3', $start_date = '2012-10-17', null, null, 10 );
$ga3->requestReportData($report_id = XXXX, $dimension = array('date'), $metrics = array('pageviews','visits'), $sort_metric = array('date') , $filter = '',$date2,$today, null,15);
因为没有存储返回值。所以你可以把它们留下来。然后结果是,不需要三个对象$ga1
到$ga3
。所以,是的,你可以真正压缩它并保留任何这些对象。此外,不需要计算日期时间。
您的新文件:
<?php
/* space intentionally left blank */
?>
故事的道德:只要您不显示为什么您调用这些函数,就不能说如何改进代码。