views:

67

answers:

2

Say hypothetically I have a class...

class Main {

    $prop1 = 2;
    $prop2 = 23;
    ...
    $prop42 = "what";

    function __construct($arg_array) {
        foreach ($arg_array as $key => $val) {
            $this->$key = $val;
            }
        }
    }

Say I create and object...


$attributes = array("prop1"=>1, "prop2"=>35235, "prop3"=>"test");
$o = new Main($attributes);

Providing for default property values if not supplied by the user is obvious. But what if I want to enforce arbitrary limits on user supplied values for object properties? What if I want to enforce $prop1 to be of int, be no less than 1, and be no greater than 5. And, $prop42 to be of type string, no less than 'A', and no greater than 'Z'? For this purpose, what would be the cleanest way, keeping the script as short and sweet as possible, using any possible language feature or trick?

I'm stuck in __construct() checking supplied values against a rule array built like so...

$allowable = array(
    "prop1" => array(
        'type' => 'int',
        'allowable_values' => array(
            'min' => 1,
            'max' => 5
            )
        ),
    "prop2" => array(
        'type' => 'int',
        'allowable_values' => array(
            1,
            235,
            37,
            392,
            13,
            409,
            3216
            )
        ),
    ...
    "prop42" => array(
        'type' => 'string',
        'allowable_values' => array(
            'min' => 'A',
            'max' => 'Z'
            )
        )
    );

As you can see by prop2, my validation function is starting to get pretty messy with so many 'if-then-iterate-again' blocks as I have to account for not only ranges but a list of permitted values. With the validation code and this rule array, my script is getting rather bulky.

The question is, how can I structure my class or class properties or the validation code or any other aspect of my script to be as short and concise as possible to allow property range and value enforcement? Is there a language feature or trick to handle this more elegantly? Have I reached a brick wall, the limit of this language? Are there any examples from other languages that can easily implement this which can provide some clue?

+1  A: 

getters and setters

class Main {
  private $prop1;
  private $prop2;
  private $prop3;

  public function __construct( $p1 , $p2 , $p3 )
  {
    $this->setProp1($p1);
    $this->setProp2($p2);
    $this->setProp3($p3);
  }

  public function setProp1($p1)
  {
    // conditional checking for prop1
    if(!$ok) throw new Exception('problem with prop1');
    $this->prop1 = $p1;
  }

  //.. and so on
}
nathan
Now he needs 84 getters and setters. Not sure if that's a good idea.
quantumSoup
The number of 'set' functions easily reduced to 0 by instead utilizing __call(). However, now I'm back to defining special cases using so many 'if-thens' inside call() to validate each case. I'm going in circles and not reducing code at all.
bob-the-destroyer
you're trying to avoid coding.. here's the thing - whatever syntax you do won't optimize the script, be verbose and strict, let it compile in to (probably the same or faster) opcodes, make debugging much easier, code cleaner, and far more maintainable long term.
nathan
There's nothing inherently wrong with verbose code.
Dolph
@nathan I'm actually hoping for a php coding example of just what you suggest. Except can you clarify "whatever syntax you do won't optimize the script".
bob-the-destroyer
see the example i posted, as for "whatever syntax you do won't optimize the script", regardless of how short and clever you try to be, the code still has to do the same work, and if anything the more condensed you make it, the more convoluted the 'real' code the machine runes (the opcodes) will be :)
nathan
A: 

I ran into a similar issue the other day. Here's what I would do:

   private $props;
   private $rules; 

   function __construct($params) {

      // or you can get the rules from another file, 
      // or a singleton as I suggested

      $this->rules = array (array('type' => 'range', 'min' => 10, 'max' => 20), 
        array('type' => 'in_set', 'allowed' => array(1,2,3)));

      for ($i=0; $i<count($params); $i++) {

         if ($this->check($params[$i], $this->rules($i))
            $this->props[$i] = $params[$i];
         else
            throw new Exception('Error adding prop ' . $i);
      }

   }


   function check($value, $rule) {
      switch($rule['type']) {
         case 'range':
            return ($rule['min'] <= $value && $value <= $rule['max']);  

         case 'in_set':
            return (in_array($value, $rule['allowed']));

         // and so on
      }
   }

If you have many parameters, you can use an array and iterate through that. If your validation rules are always going to be the same, you can put them in a separate file and load that in your constructor or whatever.

EDIT: By the way, there is really no point in testing type in PHP. It is both not very reliable and unnecessary.

EDIT 2: Instead of having a global variable with the rules, you can use a Singleton:

quantumSoup
now he needs a big monolith of 84 if/else's in his constructor.. - ie why go oo for that?
nathan
No he doesn't. I'll edit that in a bit.
quantumSoup
@Aircule Your example is basically what I'm doing now. My validation code is placed in my `__construct()` function, and I have the rule array somewhere else to keep down the clutter. I guess what I'm hoping for is some language feature or trick that helps in this case.
bob-the-destroyer
@Aircule Using count() inside a loop is a 'Bad Idea' when the array will be the same size every iteration. Doing so can really slow down a script that has longer loops in it. Even if a particular loop will be small, it's still a good idea to do it properly, to avoid having the bad habit.
tsgrasser
@tsgrasser: That was merely to demonstrate the concept and the algorithm. If one really needs to perform optimizations like that, we might as well all go back to hacking code in C and use pointer hacks to iterate through arrays.
quantumSoup
@tsgrasser Not important for this question. It's the overall concept that counts, and examples just help to explain it.
bob-the-destroyer