I want to integrate the Google Analytics and Marchex (call tracking system) Call Analytics API's to pull custom reports with conversion rates and that sort of thing. I am very, very new to the world of Object Oriented programming, and think I need some guidance getting this off the ground. I would like some critique on the constructor before I move on any further. Am I doing this right or what?
<?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
?>
You assigned some non-static variable using ->$ syntax, which wasn't OK. Here's a fixed proposal:
<?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
精彩评论