Adsense.class.php

Discussion in 'AdSense' started by cornelius, Jan 1, 2006.

  1. #1
    heres a little Object Oriented PHP5 class for adsense that i made and use, it aint the best but does great job at serving ads on thousands of pages i have :)

    feel free to modify and improve

    <?php
    /**
     * @name Adsense.class.php
     * @abstract object for handling adsense adverts
     * @access 	public
     * @author	FN
     */
    class Adsense{
    
    	static $ad_client;
    	
    	/**
    	 * private attributes
    	 */
    	private $ad_channel,
    			$ad_width,
    			$ad_height,
    			$ad_format,
    			$output;
    
    	/**
    	 * constructor
    	 */
    	public function __construct(){
    
    		//set variables
    		$this->ad_channel = '';
    		$this->ad_width = 0;
    		$this->ad_height = 0;
    		$this->ad_format = '';
    		$this->output = '';
    	}
    
    
    	/**
    	 * getters and setters
    	 */
    
    	public function setAd_channel( $ad_channel ){
    		$this->ad_channel = $ad_channel;
    	}
    
    	public function getAd_channel(){
    		return $this->ad_channel;
    	}
    
    	public function setAd_width( $ad_width ){
    		$this->ad_width = $ad_width;
    	}
    
    	public function getAd_width(){
    		return $this->ad_width;
    	}
    
    	public function setAd_height( $ad_height ){
    		$this->ad_height = $ad_height;
    	}
    
    	public function getAd_height(){
    		return $this->ad_height;
    	}
    
    	public function setAd_format( $ad_format ){
    		$this->ad_format = $ad_format;
    	}
    
    	public function getAd_format(){
    		return $this->ad_format;
    	}
    
    	
    	public function setAdvert( $channel, $width, $height, $format ){
    		$this->setAd_channel( $channel );
    		$this->setAd_width( $width );
    		$this->setAd_height( $height );
    		$this->setAd_format( $format );
    	}
    
    
    	/**
    	 * construct the ad
         * @access public
         * @param array $_ADSENSE_COLORS
         * @return string $advert
    	 */
    	public function make($_ADSENSE_COLORS){
    
    		//construct advert
          $this->output = '	
    <script type="text/javascript">
    <!--
       	google_ad_client = "'.self::$ad_client.'";
       	google_ad_width = "'.$this->getAd_width().'";
    	google_ad_height = "'.$this->getAd_height().'";
    	google_ad_format = "'.$this->getAd_format().'";
    	google_ad_type = "text_image";
    	google_ad_channel = "'.$this->getAd_channel().'";
    	google_color_border = "'.$_ADSENSE_COLORS['border'].'";
    	google_color_bg = "'.$_ADSENSE_COLORS['bg'].'";
    	google_color_link = "'.$_ADSENSE_COLORS['link'].'";
    	google_color_url = "'.$_ADSENSE_COLORS['url'].'";
    	google_color_text = "'.$_ADSENSE_COLORS['text'].'";
    //-->
    </script>
    <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';
    	}
    	
    	
    	public function toString(){
    		return $this->output;
    	}
    }
    PHP:
    heres sample use
    <?php
    
    include_once('Adsense.class.php');
    
    Adsense::$ad_client			=	$_SETTINGS['adsense_publisher'];
    
    		$ad1 = new Adsense();
    		$ad1->setAdvert('0123456789', 160, 600, '160x600_as');
    		$ad1->make($_ADSENSE_COLORS);
    
    
    $ad1->toString();
    
    ?>
    PHP:
     
    cornelius, Jan 1, 2006 IP
  2. Bliss

    Bliss Peon

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In the example, you don't define $_SETTINGS and $_ADSENSE_COLORS. ;)
     
    Bliss, Jan 2, 2006 IP
  3. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    oops i they just arrays used for configurations

    
    $_SETTINGS['adsense_publisher'] = 'pub-xxxyyyzzzooooo';
    
    
    $_ADSENSE_COLORS = array(	'border'	=>	'f1faf8',
    					       'bg'	       =>	'f1faf8',
    							'link'		=>	'47a4be',
    							'url'		=>	'000066',
    							'text'		=>	'000066'	);
    
    PHP:
     
    cornelius, Jan 2, 2006 IP
  4. Bliss

    Bliss Peon

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Better now, thank you for sharing. You might want to post that to phpclasses.org as well. ;)
     
    Bliss, Jan 2, 2006 IP