Re: Updating inherited attributes without __construct()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sorry, I probably should have included the add() function from the parent. As well as all of the parent constructor.

Basically the object takes an array of key=>value pairs and parses them into a string for output. When it takes in the array of pairs it needs to check the keys against a list of valid keys. I have no preference on how to store the list of valid keys other than to add more a constructor doesn't need to be called.

class parent{
  $validkeys = 'name,size';
  $attributes = array();

  __construct($set){
    foreach($set as $key=>$value){
      if(isValidKey($key)){
        $this->attributes[$key] = $value;
      }
    }
  }

  __toString(){
    $output = "";
    foreach($this->attributes as $key=>value){
      $output .= sprintf(TEMPLATE, $key, $value);
    }
  }

  __set($key, $value){
    if(isValidKey($key)){
      $this->attributes[$key] = $value;
    }
  }

  isValidKey($key){
  ...Something goes here...
  }
}

class child extends parent{
...All this needs to do is tack on values 'color,shape' to parent's valid keys...
}

class grandchild extends child{
...All this needs to do is tack on values 'cost,location' to child's valid keys...
}

Most sub classes won't need anything different from their parent but the expanded list of valid keys.

Nathan Nobbe wrote:
i dont quite understand this reasoning.. if you want to add some valid keys,
based upon the design of the parent, it seems to me the children do have a
need for an overridden constructor.  if you have accessor methods w/ a
consistent naming convention, you could omit the $validKeys array
altogether, and determine if a given field is valid based upon runtime
introspection.  something like this,

/// in the parent class
protected function isFieldValid($field) {
  if(method_exists($this, 'get' . ucfirst($field)))
    return true;
  else
    return false;
}

this will work in all the child classes as well, as long as you stick to a
consistent naming convention, and you can of course add a check for a set..
method as well if you like.  im not saying this technique is the holy grail
or anything, im merely offering it as an alternative to your current
solution.

-nathan

> On Thu, Jun 5, 2008 at 12:15 PM, Tyson Vanover <tvanover@xxxxxxx> wrote:
>
>> I have a class that has a list of valid keys, and an array of values. When >> a value is added to the array it's key is first checked against the list of >> valid keys (this is to prevent injection issues we have been having later on
>> in the project).
>>
>>  class parent{
>>    private $validkeys = 'title,color,name';
>>    private $values = array();
>>  }
>> That class is inherited by other classes that mostly just have an expanded >> list of valid keys. I would like to be able to update the valid key list >> without having to craft a constructor for child objects. I would rather not
>> give each child class the constructor,
>>
>>  __construct()
>>  {
>>    $this->validkeys.= ',setting,...';
>>    parent::__construct();
>>  }
>>
>> since most child classes have no need of a unique constructor.

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux