self, static, this

<?php

/**
 * Physical address. 
 */
abstract class Address implements Model {
  
  const ADDRESS_TYPE_RESIDENCE = 1;
  const ADDRESS_TYPE_BUSINESS = 2;
  const ADDRESS_TYPE_PARK = 3;
  
  // Address types.
  static public $valid_address_types = array(
    Address::ADDRESS_TYPE_RESIDENCE => 'Residence',
    Address::ADDRESS_TYPE_BUSINESS => 'Business',
    Address::ADDRESS_TYPE_PARK => 'Park',
  );
  
  // Street address.
  public $street_address_1;
  public $street_address_2;
  
  // Name of the City.
  public $city_name;
  
  // Name of the subdivison.
  public $subdivision_name;
  
  // Postal code.
  protected $_postal_code;
  
  // Name of the Country.
  public $country_name;
  
  // Primary key of an Address.
  protected $_address_id;
  
  // Address type id.
  protected $_address_type_id;
  
  // When the record was created and last updated.
  protected $_time_created;
  protected $_time_updated;
  
  /**
   * Post clone behavior. 
   */
  function __clone() {
    $this->_time_created = time();
    $this->_time_updated = NULL;
  }
  
  /**
   * Constructor.
   * @param array $data Optional array of property names and values.
   */
  function __construct($data = array()) {
    $this->_init();
    $this->_time_created = time();
    
    // Ensure that the Address can be populated.
    if (!is_array($data)) {
      trigger_error('Unable to construct address with a ' . get_class($name));
    }
    
    // If there is at least one value, populate the Address with it.
    if (count($data) > 0) {
      foreach ($data as $name => $value) {
        // Special case for protected properties.
        if (in_array($name, array(
          'time_created',
          'time_updated',
          'address_id',
          'address_type_id',
        ))) {
          $name = '_' . $name;
        }
        $this->$name = $value;
      }
    }
  }
  
  /**
   * Magic __get.
   * @param string $name 
   * @return mixed
   */
  function __get($name) {
    // Postal code lookup if unset.
    if (!$this->_postal_code) {
      $this->_postal_code = $this->_postal_code_guess();
    }
    
    // Attempt to return a protected property by name.
    $protected_property_name = '_' . $name;
    if (property_exists($this, $protected_property_name)) {
      return $this->$protected_property_name;
    }
    
    // Unable to access property; trigger error.
    trigger_error('Undefined property via __get: ' . $name);
    return NULL;
  }
  
  /**
   * Magic __set.
   * @param string $name
   * @param mixed $value 
   */
  function __set($name, $value) {
    // Allow anything to set the postal code.
    if ('postal_code' == $name) {
      $this->$name = $value;
      return;
    }
    
    // Unable to access property; trigger error.
    trigger_error('Undefined or unallowed property via __set(): ' . $name);
  }
  
  /**
   * Magic __toString.
   * @return string 
   */
  function __toString() {
    return $this->display();
  }
  
  /**
   * Force extending classes to implement init method. 
   */
  abstract protected function _init();
  
  /**
   * Guess the postal code given the subdivision and city name.
   * @todo Replace with a database lookup.
   * @return string 
   */
  protected function _postal_code_guess() {
    $db = Database::getInstance();
    $mysqli = $db->getConnection();
    
    $sql_query  = 'SELECT postal_code ';
    $sql_query .= 'FROM location ';
    
    $city_name = $mysqli->real_escape_string($this->city_name);
    $sql_query .= 'WHERE city_name = "' . $city_name . '" ';
    
    $subdivision_name = $mysqli->real_escape_string($this->subdivision_name);
    $sql_query .= 'AND subdivision_name = "' . $subdivision_name . '" ';
    
    $result = $mysqli->query($sql_query);
    
    if ($row = $result->fetch_assoc()) {
      return $row['postal_code'];
    }
  }
  
  /**
   * Display an address in HTML.
   * @return string 
   */
  function display() {
    $output = '';
    
    // Street address.
    $output .= $this->street_address_1;
    if ($this->street_address_2) {
      $output .= '<br/>' . $this->street_address_2;
    }
    
    // City, Subdivision Postal.
    $output .= '<br/>';
    $output .= $this->city_name . ', ' . $this->subdivision_name;
    $output .= ' ' . $this->postal_code;
    
    // Country.
    $output .= '<br/>';
    $output .= $this->country_name;
    
    return $output;
  }
  
  /**
   * Determine if an address type is valid.
   * @param int $address_type_id
   * @return boolean
   */
  static public function isValidAddressTypeId($address_type_id) {
    return array_key_exists($address_type_id, self::$valid_address_types);
  }
  
  /**
   * If valid, set the address type id.
   * @param int $address_type_id 
   */
  protected function _setAddressTypeId($address_type_id) {
    if (self::isValidAddressTypeId($address_type_id)) {
      $this->_address_type_id = $address_type_id;
    }
  }
  
  /**
   * Load an Address.
   * @param int $address_id 
   */
  final public static function load($address_id) {
    $db = Database::getInstance();
    $mysqli = $db->getConnection();
    
    $sql_query = 'SELECT address.* ';
    $sql_query .= ' FROM address ';
    $sql_query .= 'WHERE address_id = "' .(int)$address_id . '" ';
    
    echo $sql_query;
    $result = $mysqli->query($sql_query);
    if ($row = $result->fetch_assoc()){
      return self::getInstance($row['address_type_id'],$row);
    }
  }
  
  /**
  * given an addresss_type_id, return an instance of that subclass
  * 
  * @param int $address_type_id
  * @param array $data
  * @return Address subclass
  */
  final public static function getInstance($address_type_id, $data=array()){
   $class_name = 'Address' . self::$valid_address_types[$address_type_id]; 
   return new $class_name($data);
  }
  
  /**
   * Save an Address. 
   */
  final public function save() {}
}

Open in new window







in this example:

I do not understand
self

is it using a static
how does self relate to this



  /**
   * Load an Address.
   * @param int $address_id 
   */
  final public static function load($address_id) {
    $db = Database::getInstance();
    $mysqli = $db->getConnection();
    
    $sql_query = 'SELECT address.* ';
    $sql_query .= ' FROM address ';
    $sql_query .= 'WHERE address_id = "' .(int)$address_id . '" ';
    
    echo $sql_query;
    $result = $mysqli->query($sql_query);
    if ($row = $result->fetch_assoc()){
      return self::getInstance($row['address_type_id'],$row);
    }
  }
  
  /**
  * given an addresss_type_id, return an instance of that subclass
  * 
  * @param int $address_type_id
  * @param array $data
  * @return Address subclass
  */
  final public static function getInstance($address_type_id, $data=array()){
   $class_name = 'Address' . self::$valid_address_types[$address_type_id]; 
   return new $class_name($data);
  }

Open in new window

LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
in short:
$this refers to the current object (the instance)
self refers to the current class (which is the class definition)

The class is the blueprint of the object.
So you define a class, but you construct objects from that class.

you use self for static and $this for none-static members or methods.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
so self is child methods or child classes from the base parent class

why must self be static
0
rgb192Author Commented:
thanks for
this: object (instance)
and
self: current class
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.