static function or method

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

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.

Ray PaseurCommented:
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';
0
rgb192Author Commented:
okay so now I understand ternary operator
but how does this work
Address::isValidAddressTypeId($id)
0
Ray PaseurCommented:
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.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

rgb192Author 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>";
}
0
Ray PaseurCommented:
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."
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 static method means no instance of an object and can be called all by itself

thanks
0
Ray PaseurCommented:
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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.