我已经和cakePHP一起工作过,并且喜欢他们构建模型系统的方式。我想将他们的想法在扩展模型之间进行处理。将子对象的属性传递给父对象PHP
下面是一个例子:
class users extends model {
var $validation = array(
"username" => array(
"rule" => "not_empty"
),
"password" => array(
"rule" => "valid_password"
)
);
public function create_user() {
if($this->insert() == true) {
return true;
}
}
}
class model {
public function insert() {
if(isset($this->validation)) {
// Do some validation checks before we insert the value in the database
}
// Continue with the insert in the database
}
}
与这里的问题是,模型没有得到验证规则,因为它的父类的方法。有没有一种方法可以将$ validation属性传递给父类,而无需通过将create_user()方法作为参数来明确地传递验证规则?
编辑:
此外,避免了经由__construct()方法的父类传递给它。是否有另外一种方法可以做到这一点,这不会在我的用户类中导致大量额外的代码,但让模型类可以完成大部分工作(如果不是全部?)