Php 面向对象编程,扩展属性
好的,这不仅仅是一个PHP问题,但是我在PHP中遇到了这个问题,所以我将用PHP发布这个例子 例如: 我有三个班,比如说,数据抽象班,个人班和学生班。现在,我有一个与每个对象相关的数据数组 class person extends data_abstract { protected $data; //name, gender, etc } class student extends person { protected $data; //student_id, etc } 类person扩展了数据抽象 { 受保护的$data;//姓名、性别等 } 班级学生延伸人 { 受保护的$data;//学生id等 } 现在,假设这些“数据”属性中的每一个都来自数据库表,例如,table_person、table_student 实例化后,我们将通过get_class()函数解析类名,并从它们的相关表中获取数据 class data_abstract { public function __construct() { $name = get_class($this); $table = 'table_' . $name; $this->data = DB->query('SELECT * FROM ' . $table); //whatever DB constant is, //I'm just trying to get all rows of data from the related table. } } 类数据抽象 { 公共函数构造() { $name=get_类($this); $table='table.'$name; $this->data=DB->query('从'.$table'中选择*); //不管DB常数是什么, //我只是想从相关表中获取所有数据行。 } } 好的,现在问题来了,当我用$student=newstudent()实例化student时;构造函数将为我从表\u student获取数据并将其放入$student->data中,但我无法从表\u person获取数据并将这两组数据放入一个对象中 通过扩展另一个类,我们可以扩展和定制所有方法(函数)(通过多态性),但是扩展每个级别对象的属性/属性似乎很困难,至少不需要一些手动构造 在抽象层有什么方法可以实现这一点吗Php 面向对象编程,扩展属性,php,oop,Php,Oop,好的,这不仅仅是一个PHP问题,但是我在PHP中遇到了这个问题,所以我将用PHP发布这个例子 例如: 我有三个班,比如说,数据抽象班,个人班和学生班。现在,我有一个与每个对象相关的数据数组 class person extends data_abstract { protected $data; //name, gender, etc } class student extends person { protected $data; //student_id, etc } 类p
(感谢阅读,希望我能把问题弄清楚。)就我个人而言,我会将数据库加载类放在一个受保护的方法中,该方法采用属性
$tablename
,并加载所有数据。然后在类的构造函数中手动调用它,然后调用父类
class Person
{
public function __construct() {
$this - > loadData("Person");
}
}
Class Student extends Person
{
public function __construct() {
parent::__construct();
$this - > loadData("Student");
}
}
现在,当您构建Student
时,将加载Student
和Person
中的数据。如果使用,两个数据数组将合并为一个
这是一个风格问题;有些人会批评这一点,说你现在重复了两次——需要更多的重构工作——但我会说你将类名和接口从数据库设计中分离出来(这是一件好事),通过父调用,代码现在更加面向对象了。就我个人而言,我将把数据库加载类放在一个受保护的方法中,该方法采用属性
$tablename
,并加载所有数据。然后在类的构造函数中手动调用它,然后调用父类
class Person
{
public function __construct() {
$this - > loadData("Person");
}
}
Class Student extends Person
{
public function __construct() {
parent::__construct();
$this - > loadData("Student");
}
}
现在,当您构建Student
时,将加载Student
和Person
中的数据。如果使用,两个数据数组将合并为一个
这是一个风格问题;有些人会批评这一点,说你现在重复了两次——重构的工作量增加了——但我要说的是,你将类名和接口与数据库设计分离(这是一件好事),通过父调用,代码现在更加面向对象了。如果我理解得好的话:
- 您希望所有的DB数据都是$data李>
- $data必须与表“table_classname”中的数据一起馈送李>
- $data“继承”自应随该进程加载到父类中的$data
Array
(
[0] => table_DataAbstract
[1] => table_Person
[2] => table_Student
)
顺便说一句,记住PHP得到了内省,它允许您动态设置所需的字段:可能是使用大数组
如果您知道所有字段的名称,您可以执行以下操作
function populate($data) // get the array from the DB
{
$fieldList = get_class_vars($this); // get the filed list
foreach ($fieldList as $key => $value)
{
$this->$key = $data[$key]; // feed the field one by one with the array
}
}
如果我理解得很好:
- 您希望所有的DB数据都是$data李>
- $data必须与表“table_classname”中的数据一起馈送李>
- $data“继承”自应随该进程加载到父类中的$data
Array
(
[0] => table_DataAbstract
[1] => table_Person
[2] => table_Student
)
顺便说一句,记住PHP得到了内省,它允许您动态设置所需的字段:可能是使用大数组
如果您知道所有字段的名称,您可以执行以下操作
function populate($data) // get the array from the DB
{
$fieldList = get_class_vars($this); // get the filed list
foreach ($fieldList as $key => $value)
{
$this->$key = $data[$key]; // feed the field one by one with the array
}
}
好吧,我不确定我是否做对了。。。$数据是否存储在关联数组中?您需要通过调用父类的超级构造函数来获取父类的数据::u construct();好吧,我不确定我是否做对了。。。$数据是否存储在关联数组中?您需要通过调用父类的超级构造函数来获取父类的数据::u construct();