CodeIgniter Forums

Full Version: Newbie Here. Classes extending Classes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

So I'm learning this OOP or MVC stuff, and CI all at the same time and just have a question.

I want to make a website with a searchable DB for all the items in a game. I'm fresh out of the short tutorial in the docs and trying to plan my project.

I'm wondering if I can or should set up my model classes like this:

class Items_model extends CI_Model {}

class Armor_model extends Items_model {}

class Helmet_model extends Armor_model {}

class Gloves_model extends Armor_model {}

class MeleeWeapons_model extends Items_model {}

class Swords_model extends MeleeWeapons_model {}

class Daggers_model extends MeleeWeapons_model {}

Or should they all extend CI_Model only, and in a separate file each?

El Forum

Welcome to the CodeIgniter forums!

I think you have the right idea. If the classes you're extending contain functionality that you need to use from certain sub-classes, that's exactly what inheritance is for. However, you will need to remember to include() the base class first. You can do that at the top of your model file, so you end up with a chain effect.

El Forum

I might have misunderstood you slightly, so I'll mention one more thing. You should only ever have one class per file:

El Forum

Thank you! Yes each child/subclass (sorry I don't know the terminology quite well yet either) shares some basic properties with the parent class but also has it's own unique properties.

Good to also know 1 file per class but include my parent class at the top of the model file.

It all makes sense, thank you Smile

I have been workign with HTML, CSS, javascript/jquery a lot in the last few weeks so I had my mind stuck around "more file includes = more http requests" but I have to remember this is all server-side lol

El Forum

It sure makes a pleasant change to find a newbie who has a fairly firm grasp on inheritance.

Your Fung-Ku is strong!