Robert Ian Smit wrote:
I am trying to implement a generic form handler that is capable of
printing the form and checking the user input.

I want this application to be useful in the end, but I also use it
to explore OOP in PHP.

..................
I'd like the zipcode object to ask a question to find out what
country we're dealing with and then apply the check of the input
depending on the answer.

Although these questions are specific to my application, I guess my
real question is more general. When you have a containerobject that
stores different kinds of elements, how do you give the elements the
ability to learn about their surroundings so they can alter their
behaviour?



Since I see you are interested in an OOP solution of your problem, I'll try to answer you taking that point of view.


Altering the object behavior depending on a given context (surrounding variable(s)) could be done easily by performing PHP's class inheritance and polymorphism.

In your case you can use an abstract class, say class ZipCode, which defines the functionality of a generic zipcode object, independent of any country standard. We call this class abstract because no objects will be created as direct instances from it.

Then for each country standard you can have a class, inherited from the ZipCode abstract class, in which the format() method is overridden so to implement appropriate for that standard reformatting algorithm.

When you create new zipcode object for a given standard, first check the context variable (say $country) and them make decision which subclass to use for creating the new zipcode object. In this way by calling the format() method of the created zipcode object, different algorithms for reformatting the zipcode provided will be implemented.

Perhaps this short explanation will be more clear considering the example code below. In this example we have an abstract class ZipCode, and 2 subclasses - ZipCodeUS and ZipCodeDE, representing the zipcode formatted accordingly (fake) 'USA' and 'Germany' standards. Check out the implementation code at the end of the example by giving 'US' and 'DE' values to the $country variable.


HTH,


Boyan
--

-- example ------------------------------------------------

<?php

  /**
   * class ZipCode
   * abstract class, never used for creating objects
   * represents a generic zipcode object
   */
  class ZipCode {

    // data members
    var $_zipcode;
    var $_zipcodeFormated;

    // constructor
    function ZipCode($value) {
      $this->_zipcode = $value;
    }

    // abstract method,
    // to be overriden in class instances
    function format(){}

    // data member accessor
    function getCode() {
      return $this->_zipcodeFormated;
    }
  }


/** * class ZipCodeDE * represents zipcode according the 'DE' standard; * incherited from ZipCode, with overriden format() method **/ class ZipCodeDE extends ZipCode {

    function format() {
      // move the number part at the begining
      $tmp  = explode(' ', $this->_zipcode);
      $tmpf = array($tmp[1], $tmp[0], $tmp[2]);
      $this->_zipcodeFormated = implode(" ", $tmpf);
    }
  }


/** * class ZipCodeUS * represents zipcode according the 'US' standard; * incherited from ZipCode, with overriden format() method **/ class ZipCodeUS extends ZipCode {

    function format() {
      // move the number part at the end
      $tmp  = explode(' ', $this->_zipcode);
      $tmpf = array($tmp[0], $tmp[2], $tmp[1]);
      $this->_zipcodeFormated = implode(" ", $tmpf);
    }
  }


//------------------------------------------------------------ // test implementation

$a_zipcode = 'AA 1234 BB';
$country = 'DE';

if ($country == 'US') {
  $ZipCode = new ZipCodeUS($a_zipcode);
  $ZipCode->format();
}

if ($country == 'DE') {
  $ZipCode = new ZipCodeDE($a_zipcode);
  $ZipCode->format();
}

echo "zipcode '" . $a_zipcode . "' formated according the '" . $country . "' standard: " . $ZipCode->getCode() . "<br>";

?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to