Zend Framework (a.k.a. ZF) is the swiss army knife of MVC based frameworks. (The discussion of ZF really being a framework versus a loosely coupled library is another discussion in its own.) Name one thing you want to do with a framework and chances are ZF already supports it out of the box and if not that is okay too, it is easily extend-able (for some reasons my blog doesn’t like intransitive verbs so pardon the hyphen). However learning how to wield ZF can seem like a daunting task. One thing I learned early on with ZF was that the curators and associates in the ZF ecosystem always fall back to the root of “read the code/api/documentation”. With good reason too! It is not the volunteers simply shrugging you off but it is for your own good. Lets take for example the use of the class Zend_Validate_File_ImageSize. If you haven’t ever used this class before a quick reading into the class name tells you it validates a file and if that file is an image, the image size. Fair enough but how do we use this class? Well I use Netbeans and the easiest way for me to open a file is to CTRL+click the class name, Netbeans does a wonderful job of bringing you right to the constructor for the class.
Now we are presented with:
/** * Sets validator options * * Accepts the following option keys: * - minheight * - minwidth * - maxheight * - maxwidth * * @param Zend_Config|array $options * @return void */ public function __construct($options) { if ($options instanceof Zend_Config) { $options = $options->toArray(); } elseif (1 < func_num_args()) { if (!is_array($options)) { $options = array('minwidth' => $options); } $argv = func_get_args(); array_shift($argv); $options['minheight'] = array_shift($argv); if (!empty($argv)) { $options['maxwidth'] = array_shift($argv); if (!empty($argv)) { $options['maxheight'] = array_shift($argv); } } } else if (!is_array($options)) { require_once 'Zend/Validate/Exception.php'; throw new Zend_Validate_Exception ('Invalid options to validator provided'); } if (isset($options['minheight']) || isset($options['minwidth'])) { $this->setImageMin($options); } if (isset($options['maxheight']) || isset($options['maxwidth'])) { $this->setImageMax($options); } }
Reading over this we can see that the constructor accepts a few different types of options. It will accept a single Zend_Config class instance, a string defining the minimum width of the image, or an array defining some or all of the options that can be set. Which options can be set? Well the ones that the code says we can use: minwidth, minheight, maxwidth, maxheight (In this order too!). I personally find using Zend_Config overkill so we could invoke the instance like so
//backwards compatibility supports this but I find this to be something that should be broke as the constructors are now unified and this could be messy $validatorA = new Zend_Validate_File_Image_Size(0,0,110,110); // suggested way of passing in parameters to the constructor $validatorB = new Zend_Validate_File_Image_Size(array('minheight'=>0, 'minwidth'=>0, 'maxheight'=>110, 'maxwidth'=>110));
Well that’s it, this technique can be used for any class in Zend Framework. ZF is flexible enough to allow you to choose how you want to build your projects (within reason). Always RTFM before asking questions, sometimes reading and finding the answer is faster than asking and waiting for the answer.