一个抽象类,然后使用数据库中的属性创建继承该抽象类的对象
我目前正在考虑创建一个抽象类Building
。然后,我希望多种不同类型的 Building
使用数据库中的属性继承此抽象。
对于继承,我通常会有这样的东西:
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 }
}
相当标准,一个标准的蓝图构建类,定义其他人应该继承和声明的内容。 假设有超过 100 种不同的建筑类型
...其中一些非常标准,但它们的所有属性都在数据库
中定义,允许管理员进入并更改其名称、成本、要求等,其中一些可能不需要定义任何特殊功能,但如果将来需要的话定义该功能会很酷。
是否可以进入数据库并使用其属性和类型填充/创建对象,而不必先定义实际文件?如果我要实际创建该文件,那么它是手动工作,并且我与文件系统相关,而不是与数据库中存储的实际建筑物相关联。
然后我可以做类似的事情:
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 }
我想问题是......有什么方法可以创建类而不必创建实际文件。我想能够检查该文件是否确实存在并在需要时添加自定义函数。
否则我必须创建 100 多个文件,然后让工厂将它们拉出来并填写。
谢谢,多米尼克
I'm currently looking at creating an abstract class Building
. I would then like multiple different types of a Building
to inherit this abstract using properties from the database.
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.
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.
Thanks, Dominic
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不编写定义,就无法定义类。对于任何计算机语言都是如此,对于 PHP 也是如此。
但是,您可以将代码放入数据库中或根据从数据库中获取的数据生成代码。
但是,每当您接近
eval
(它将用于这些)时,看看您在创建设计时是否提出了正确的问题通常更有价值。在我看来,你一直拥有相同的对象,但它只有一些不同的数据。
我不确定这些对象必须具有多少种不同的功能。您没有分享太多有关应用程序的一般结构和底层域逻辑的信息,因此很难在第一个答案中分辨出来。
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.
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.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.