对象从其他类继承变量

时间:2015-10-29 08:56:13

标签: php oop object inheritance measurement-protocol

我正在使用PHP进行Google电子商务跟踪课程。本课程的目的是创建使用cURL发送到Google Analytics的网址。

我首先使用以下参数创建一个事务(url):

  • 的client_id
  • UA
  • 的transaction_id
  • 从属关系

然后我添加交易项目(产品是购物车)。

我希望该项继承上面的前四个参数。所以我不需要每次都重新设置它们。

电子商务跟踪类

class EcommerceTracking {

    protected $_urls = array();
    protected $_items = array();

    /* 
     * The Protocol version. The current value is '1'. 
     * This will only change when there are changes made that are not backwards compatible. 
     */
    protected $_v = '1';

    /* 
     * The tracking ID / web property ID. The format is UA-XXXX-Y. 
     * All collected data is associated by this ID. 
     */
    protected $_tid = '';   

    /* 
     * This anonymously identifies a particular user, device, or browser instance. 
     * For the web, this is generally stored as a first-party cookie with a two-year expiration. 
     */
    protected $_cid = 0;

    /* 
     * The type of hit 
     */
    protected $_t = 'transaction';

    /* A unique identifier for the transaction. This value should be the same for both the 
     * Transaction hit and Items hits associated to the particular transaction. 
     */
    protected $_ti = 0; 

    /* 
     * Specifies the affiliation or store name. 
     */
    protected $_ta = 'online';

    /*
     * Specifies the total revenue associated with the transaction. 
     */
    protected $_tr = 0;

    /*
     * Specifies the total shipping cost of the transaction. 
     */
    protected $_ts = 0;

    /*
     * Specifies the total tax of the transaction.
     */
    protected $_tt = 0;

    /*
     * When present indicates the local currency for all transaction currency values. 
     * Value should be a valid ISO 4217 currency code.
     */
    protected $_cu = 'EUR';


    public function __contruct( $params ) {
        $this->_tid     = $params['ua'];
        $this->_cid     = $params['client_id'];
        $this->_ti  = $params['transaction_id'];
        $this->_ta  = $params['affiliation'];
        $this->_tr  = number_format( $params['revenue'], 2 );
        $this->_ts  = number_format( $params['shipping'], 2 );
        $this->_tt  = number_format( $params['tax'], 2 );       
    }

    public function addItem( TrackingItem $item ) {
        $this->_items[]     = $item;
        $this->_urls[]  = $item->getUrl();
    }

    public function getItems() {
        return $this->_items;   
    }

    public function getUrls() {
        return $this->_urls;    
    }

    public function send() {

        if( ! empty( $this->_urls ) ) {
            foreach( $this->_urls as $url ) {

                $ch = curl_init();              
                curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );  
                curl_setopt( $ch, CURLOPT_URL, 'http://www.google-analytics.com/collect' ); 
                curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
                curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); 
                curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY ); 
                curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2; .NET CLR 2.0.50727)' );
                curl_setopt( $ch, CURLOPT_POSTFIELDS, $url ); 
                curl_exec( $ch );
                curl_close( $ch );

            }

        }

    }

}

TrackingItem类

class TrackingItem {

    // The type of hit.
    protected $_t = 'item';

    // Specifies the item name.
    protected $_in;

    // Specifies the price for a single item / unit.
    protected $_ip = 0;

    // Specifies the number of items purchased.
    protected $_iq = 1;

    // Specifies the SKU or item code.
    protected $_ic;


    public function __construct( $name, $price, $qty, $sku ) {
        $this->_in  = $name;
        $this->_ip  = number_format( $price, 2 );
        $this->_iq  = $qty;
        $this->_ic  = $sku;
    }

    public function getName() {
        return $this->_in;  
    }

    public function getUrl() {
        $url = 'v=' . $this->_v . '&tid=' . $this->_tid . '&cid=' . $this->cid . '&t=' . $this->_t . 
        '&ti=' . $this->_ti . '&in=' . $this->_in . '&ip=' . $this-_ip . '&iq=' . $this->_iq . '&ic=' . $this->_ic; 
    }

}

创建对象

// Create tracking object
$tracking = new EcommerceTracking( array(
    'ua'                => 'UA-XXXX',
    'client_id'         => '10.0101.010.0101',
    'transaction_id'    => 123,
    'affiliation'       => 'Shop X',
    'revenue'           => 100,
    'shipping'          => 20,
    'tax'               => 20,
));

// Add tracking items
foreach( $items as $item ) {
    $tracking->addItem( new TrackingItem( 'Name', 34, 1, 'SKU001' ) );
}

所以我不想再为该项设置'ua','client_id','transaction_id'和'affiliation'。我希望该项从跟踪对象继承。

0 个答案:

没有答案