简体   繁体   English

一个抽象类,然后使用数据库中的属性创建继承该对象的对象

[英]An abstract class and then creating objects inheriting this using properties from the database

I'm currently looking at creating an abstract class Building . 我目前正在寻找创建一个抽象类Building I would then like multiple different types of a Building to inherit this abstract using properties from the database. 然后,我希望Building多种不同类型可以使用数据库中的属性继承此摘要。

With inheritance I would usually have something like: 有了继承,我通常会得到以下内容:

abstract class Building {
    protected
        $id,
        $name,
        $cost;

    public function getName() { // return name }

}
class CoffeeShop inherits Building {
    protected $id = 2, $name = 'Coffee Shop', $cost = '£15';
    public function sellCoffee() { // sell it }
}
class ConferenceCentre inherits Building {
    protected $id = 2, $name = 'Conference Centre', $cost = '£10';
    public function bookRoom() { // book it } 
}

Pretty standard, a standard blueprint building class that defines what the others should inherit and declare. 漂亮标准,一个标准的蓝图构建类,定义了其他类应该继承和声明的内容。 Let's say there are over 100 different building types ... some of them are pretty standard but all of their properties are defined in the database , allowing an admin to go in and change it's name, costs, requirements etc and some of those may not need to define any special functionality but it would be cool to define that functionality if ever needed in the future. 假设有over 100 different building types ...其中有些是非常标准的,但是它们的所有属性都在database中定义,允许管理员进入并更改其name, costs, requirements等,其中一些可能不需要定义任何特殊功能,但将来定义该功能很酷。

Would it be possible to go into the database and fill/create an object with it's properties and type without having to define the actual file first? 是否可以进入数据库并使用其属性和类型填充/创建一个对象,而不必先定义实际文件? If I was to actually create the file then it's manual work and I'm tied to the file system rather than the actual buildings stored in the database. 如果要实际创建文件,则它是手动工作,并且与文件系统绑定,而不是与数据库中存储的实际建筑物绑定。

Then I could do something like: 然后我可以做类似的事情:

abstract class Building { // blah }

// Loop through DB to create objects that inherit the building class (factory?)
$building = new BuildingFactory('CoffeeShop');

// Random function that hints of the type (despite class file not existing)
public function (CoffeeShop $cs) { // Do stuff }

// Random test of type
if (gettype($building) == 'CoffeeShop') { // Do stuff }

I guess the question is... is there any way I can create classes without having to create the actual files. 我想问题是...有什么方法可以创建类而不必创建实际文件。 And I guess with the ability to check if that file actually exists and add custom functions if needed. 我猜想,它可以检查该文件是否确实存在,并在需要时添加自定义功能。

Otherwise I'd have to create 100+ files and have the factory pull them up and fill them in. 否则,我将不得不创建100多个文件,并让工厂将它们拉起并填充。

Thanks, Dominic 谢谢,多米尼克

You can't define a class without writing the definition. 您必须编写该定义才能定义一个类。 That's true for any computer language, and that's true for PHP as well. 任何计算机语言都是如此,PHP也是如此。

You can place the code into the database or generate the code based on data you fetch from your database however. 您可以将代码放入数据库中,也可以根据从数据库中获取的数据生成代码。

But whenever you're coming close to eval (which would be used for those), it's most often more valuable to look if you're asking the right questions when you create your design. 但是,每当您接近eval (将用于eval )时,在创建设计时查看是否要询问正确的问题通常更有价值。

It looks more to me that you have the same object all the time, but it only have some different data. 在我看来,您一直都有相同的对象,但它只包含一些不同的数据。

And I'm not sure how many different functions those object(s) must have. 而且我不确定这些对象必须具有多少个不同的功能。 You have not shared much information about the general structure of your application and the underlying domain logic, so it's hard to tell within this first answer. 您尚未共享有关应用程序的一般结构和基础域逻辑的太多信息,因此很难在第一个答案中分辨出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM