• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie Here. Classes extending Classes

#1
[eluser]Unknown[/eluser]
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:

Code:
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?

#2
[eluser]TheFuzzy0ne[/eluser]
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.

#3
[eluser]TheFuzzy0ne[/eluser]
I might have misunderstood you slightly, so I'll mention one more thing. You should only ever have one class per file: http://ellislab.com/codeigniter/user-gui..._per_class

#4
[eluser]Unknown[/eluser]
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

#5
[eluser]TheFuzzy0ne[/eluser]
It sure makes a pleasant change to find a newbie who has a fairly firm grasp on inheritance.

Your Fung-Ku is strong!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.