1

除了我拥有的这种简化方式之外,是否可以多次实例化同一个类?

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/上看到

4

5 回答 5

4

您只需要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);
于 2012-11-09T15:52:15.833 回答
0

好吧,可能会有所帮助:

$ga1 = new gapi('XXXX@gmail.com','XXXX');
$ga2 = clone $ga1;
于 2012-11-09T15:56:02.593 回答
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);

无需多次实例化同一个对象。

于 2012-11-09T15:55:30.443 回答
0

最大的问题是:多次实例化 gapi 类有意义吗?它是您要解决的“问题”的有效解决方案吗?在大多数情况下,您可以实例化同一个对象数千次,并且不会产生太大的影响(内存方面)。那些告诉您将所有对象合并到一个实例中的人完全忽略了 OOP 的意义。如果拥有同一个类的多个实例是有意义的,请不要为了微优化而更改该类。

于 2012-11-09T16:00:33.643 回答
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 */
?>

故事的寓意:只要你不说明为什么调用这些函数,就不能说如何改进代码。

  1. 返回的数据会发生什么?
  2. 您是否在查询不同的 API 帐户?
于 2012-11-09T16:02:54.673 回答