static function or method

rgb192
rgb192 used Ask the Experts™
on
class.Address.inc
<?php

/**
 * Physical address. 
 */
class Address {
  
  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;
  
  // When the record was created and last updated.
  protected $_time_created;
  protected $_time_updated;
  
  /**
   * Constructor.
   * @param array $data Optional array of property names and values.
   */
  function __construct($data = array()) {
    $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',
        ))) {
          $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('function: '.__function__.' method: '.__METHOD__.' class: '.__CLASS__ .' Undefined property via __get: ' . $name);
    return NULL;
  }
  
  /**
   * Magic __set.
   * @param string $name
   * @param mixed $value 
   */
  function __set($name, $value) {
    //only set valid address type id
    if('address_type_id' == $name){
      $this->_setAddressTypeId($value);
      return;
    }
    
    // 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();
  }
  
  /**
   * Guess the postal code given the subdivision and city name.
   * @todo Replace with a database lookup.
   * @return string 
   */
  protected function _postal_code_guess() {
    return 'LOOKUP';
  }
  
  /**
   * 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;
  }
  
  static public function isValidAddressTypeId($address_type_id){
    return 5;
    return array_key_exists($address_type_id, self::$valid_address_types);
  }
  
  protected function _setAddressTypeId($address_type_id){
    if(self::isValidAddressTypeId($address_type_id)){
      $this->_address_id = $address_type_id;
    }
  }
}

Open in new window



demo.php
<?php

require 'class.Address.inc';

echo '<h2>Instantiating Address</h2>';
$address = new Address;

echo '<h2>Empty Address</h2>';
echo '<tt><pre>' . var_export($address, TRUE) . '</pre></tt>';

echo '<h2>Setting properties...</h2>';
$address->street_address_1 = '555 Fake Street';
$address->city_name = 'Townsville';
$address->subdivision_name = 'State';
$address->postal_code = '12345';
$address->country_name = 'United States of America';
$address->address_type_id = 1;
echo '<tt><pre>' . var_export($address, TRUE) . '</pre></tt>';

echo '<h2>Displaying address...</h2>';
echo $address->display();

echo '<h2>Testing magic __get and __set</h2>';
unset($address->postal_code);
echo $address->display();

echo '<h2>Testing Address __construct with an array</h2>';
$address_2 = new Address(array(
  'street_address_1' => '123 Phony Ave',
  'city_name' => 'Villageland',
  'subdivision_name' => 'Region',
  'postal_code' => '67890',
  'country_name' => 'Canada',
));
echo $address_2->display();

echo '<h2>Address __toString</h2>';
echo $address_2;

echo '<h2>Displaying address types...static</h2>';
echo '<tt><pre>' . var_export(Address::$valid_address_types, TRUE) . '</pre></tt>';
echo '<h2>Displaying addresss types...instance</h2>';
echo '<tt><pre>' . var_export($address->valid_address_types, TRUE) . '</pre></tt>';

echo '<h2>Testing address type ID validation</h2>';
for($id=0;$id<=4;$id++){
  echo "<div>$id: ";
  echo Address::isValidAddressTypeId($id) ? 'Valid' : 'Invalid';
  echo "</div>";
}

Open in new window




when I have the static (is this a function or method??) return 5 then the check always works

please explain
echo Address::isValidAddressTypeId($id) ? 'Valid' : 'Invalid';

  static public function isValidAddressTypeId($address_type_id){
    return 5;
    return array_key_exists($address_type_id, self::$valid_address_types);
  }


echo '<h2>Testing address type ID validation</h2>';
for($id=0;$id<=4;$id++){
  echo "<div>$id: ";
  echo Address::isValidAddressTypeId($id) ? 'Valid' : 'Invalid';
  echo "</div>";
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
This is the "ternary operator" form of if() statement.

PHP goes to the Address class and passes the $id variable to the isValidAddressTypeId() method.  Then it takes the return value from the method and continues with the statement
Address::isValidAddressTypeId($id)

If the return value is evaluated to be "truthy", this part is executed
? 'Valid'

And if it's "falsy" this part is executed
: 'Invalid';

Author

Commented:
okay so now I understand ternary operator
but how does this work
Address::isValidAddressTypeId($id)
Most Valuable Expert 2011
Top Expert 2016

Commented:
The Address class contains the isValidAddressTypeId() method.  The statement calls that method almost as if it were a function defined in the global scope, by using the scope resolution operator.  The result is fed into the rest of the statement.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
but if it is static (1 constant value), why are there 4 values ($i)

for($id=0;$id<=4;$id++){
  echo "<div>$id: ";
  echo Address::isValidAddressTypeId($id) ? 'Valid' : 'Invalid';
  echo "</div>";
}
Most Valuable Expert 2011
Top Expert 2016
Commented:
The for() loop sends different values to the method named Address::isValidAddressTypeId() and the method can be called any number of times.  "Static" does not mean there is one constant value when the term "static" is used in the context of a method.  It means there is one method that does not need to be an instance of an object.  The static methods can be called "all by itself."

Author

Commented:
so static method means no instance of an object and can be called all by itself

thanks
Most Valuable Expert 2011
Top Expert 2016

Commented:
I might characterize it more like like, "No instance of an object is required for the method to be called by itself."

Thanks for the points, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial