简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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