首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将2个API集成到1个php类中(面向对象的新手)

将2个API集成到1个php类中(面向对象的新手)
EN

Stack Overflow用户
提问于 2011-02-18 06:20:57
回答 1查看 553关注 0票数 0

我想集成谷歌分析和Marchex (呼叫跟踪系统)呼叫分析API的,以拉与转换率和类似的自定义报告。我对面向对象编程的世界非常、非常陌生,我认为我需要一些指导才能让这一切开始。在我继续下一步之前,我想对构造器做一些评论。我这样做对不对?

代码语言:javascript
复制
<?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
?>
EN

回答 1

Stack Overflow用户

发布于 2011-02-18 06:50:05

您使用->$语法赋值了一些非静态变量,这是不正常的。这是一个固定的建议:

代码语言:javascript
复制
<?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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5035426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档