Re: Updating inherited attributes without __construct()

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

 



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.


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

[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