将2个API集成到1个php类中(OOP新手)

时间:2011-02-17 22:20:57

标签: php oop api class google-analytics

我想整合Google Analytics和Marchex(通话跟踪系统)Call Analytics API来提取转换率和类似事件的自定义报告。我对面向对象编程的世界非常非常新,并认为我需要一些指导才能实现这一目标。在继续前进之前,我想对构造函数进行一些批评。我这样做是对还是什么?

<?php
require_once("gapi.class.php");
require_once("xmlrpc.inc");
class garchex
{
    private $marchex_credentials = array(),
            $ga_credentials = array();
    private $marchex_account, $ga_account;
    public function __construct($marchex_credentials,$ga_credentials) {
        assert(is_array($marchex_credentials));
        assert(is_array($ga_credentials));

        //setup marchex stuff
        $this->marchex_credentials = $marchex_credentials;
        $this->marchex_account = new xmlrpc_client("/api/xmlrpc/1", "api.voicestar.com");
        $this->marchex_account->SetCredentials($marchex_credentials[0],$marchex_credentials[1]);

        //google analytics stuff
        $this->ga_credentials = $ga_credentials;
        $this->ga_account = new gapi($ga_credentials[0],$ga_credentials[1]);
    } // __construct
} // class garchex
?>

1 个答案:

答案 0 :(得分:1)

您使用 - &gt; $语法分配了一些非静态变量,但这并不正常。这是一个固定的提案:

<?php

// Better use require_once, in case someone loads the file again
require_once( "gapi.class.php" );
require_once( "xmlrpc.inc" );

class garchex
{
    private 
       $marchex_credentials = array(),
       $ga_credentials      = array()
       ;

    public function __construct( $marchex_credentials, $ga_credentials ) {

        // check, if values are fine
        assert( is_array( $marchex_credentials ) );
        assert( is_array( $ga_credentials ) );

        //setup marchex stuff
        $this->marchex_credentials = $marchex_credentials;

        $marchex_account = new xmlrpc_client("/api/xmlrpc/1", "api.voicestar.com");
        $marchex_account->SetCredentials($marchex_credentials[0],$marchex_credentials[1]);

         // google analytics stuff
         // No $ to access non-static ivars
         $this->ga_credentials = $ga_credentials;

        // black hole: local variable assigned.
        // You won't be able to access this from outside this method.
        $ga_account = new gapi($ga_credentials[0],$ga_credentials[1]);

    } // __construct
} // class garchex

// better drop the closing PHP tag