AlexMorley-Finch
AlexMorley-Finch

Reputation: 6955

should i upgrade this model to inheritance?

I'm expanding my php and general coding knowledge by writing my own 'simple' CMS system.

My database structure follows:

USERS --< WEBSITES --< PAGES

In my logic, I have three classes, a User, Website and Page class. NOT INHERITED

The User class has all the attributes from the USER TABLE plus a $_website attribute,

The Website class has all the attributes of the WEBSITE TABLE plus an ARRAY of Page objects

The way I have decided to work this is, on page load, these classes will be created and filled with the database data.

So then in the html I can echo out results simply by calling the class like so:

$USER -> name;
$USER -> website -> get_pages();
$USER -> website -> pages[0] -> get_header();

I Realised that these classes could be inherited, so Page inherits Website and Website inherits User

But if I do it that way, then I'll instantiate the Page class like:

$PAGES = array(); 
$PAGES[] = new Page( $constructor_data_to_construct_user_website_and_page );

Now IF I do it this way, the object $PAGES[0] will have all the info to do with that page, and all the info to do with the Website (first parent), and all the info to do with the User ( second parent )

If i then create another Page, i'm just copying the same User and Website details Yes?

So if i were to var_dump $PAGES[0] id get ALL the info to do with USER, WEBSITE and PAGE

If i were to var_dump $PAGES[1] id get ALL THE SAME information for USERS AND WEBSITE, but just the different Page data.

So inheritance, is causing unnecessary copying of the User and Website objects

Because I could essentially do:

$PAGES[0] -> username // username is a property of the Users class
$PAGES[1] -> username

both would return EXACTLY the same value.

surely this is bad because data is being unnecessarily copied!

Is This Correct?

Or is this question a load of rubbish and this isn't the kind of situation inheritance is used?

Upvotes: 0

Views: 67

Answers (4)

Jon
Jon

Reputation: 437424

This is exactly the sort of situation where you must not use inheritance.

Inheritance is meant to model an IS-A relationship. Typical example: a Student is a type of Person, so Student inherits from Person.

Inheritance is not, not, NOT meant to model a HAS-A relationship. Typical example: a Car has an Engine, but Car is not a type of Engine.

When we have a HAS-A relationship (e.g. a User has Website) you should almost in all cases use composition instead of inheritance:

Bad

class Website extends User {}

Good

class Website {
    private $users;
}

Upvotes: 3

user1303559
user1303559

Reputation: 436

if you declare website and user fields as "static" in their classes:

class Website extends User {
    protected static $_xxx = '123';
}

class User {
    protected static $_username = 'Fred';
}

then they will not be copied, but will be Exactly the same value.

But this is wrong do inheritance in that palce - its tied objects, but not inherited. So you must dont use inheritance in this case - parent class must be superset of child class.

Upvotes: 0

kba
kba

Reputation: 19466

No, this is not correct. You should only inherit when one is a subtype of another one.

For instance, if you were representing people, you might have a relation like Manager inherits Employee, because a Manager is a specific type of Employee. A Website is not a specific type of User, so inheritance would not make sense.

What you instead are looking for is a simple has-a relationship (aggregation), since a website may have many pages, and a user may have many websites.

Upvotes: 0

TheOx
TheOx

Reputation: 2228

Inheritance is used when a subclass is needed that is basically a specific type of the parent class. So for example a hierarchy of Animal -> Mammal -> Horse would be a reasonable inheritance hierarchy since horses are specific types of mammals and mammals are specific types of animals.

In your case I would not use inheritance. A website contains pages but pages are not an extension of a website per-se, and a user is a totally different thing altogether. When making a decision you usually use "is-a" vs. "has-a", as in

a Page is a Website vs. a Website has a Page

In this case I think the latter (has-a) is more descriptive. If the phrase "is-a" fits better then it may be a good candidate for inheritance.

Upvotes: 0

Related Questions