Welcome Guest, Not a member yet? Register   Sign In
Where should I put this class?
#1

[eluser]Rincewind[/eluser]
I'm a bit confused...

I've started building a site which includes many many stores info.
I thought maybe to create a new classe with all store attributes and function, but where should I do it? as a library and then load it like $this->load->library('store'); ? but if I do so I can't crate new objects in this way: $myObject = new Store();.

As you can see I'm really confused. Can you help me? How would you do that?

Thanks!
#2

[eluser]bretticus[/eluser]
Why do you need multiple instances of a Store within the same action or controller (method or class?)
#3

[eluser]Rincewind[/eluser]
Hmmmmmm... Now that I rethink about it I can manage with one.
OK, let's say that I want to get all stores, and I have a function inside the Store class, called get_all_stores(); What do you think it should do? return an array with stores object? each object represents a store? isn't it inefficient? an object like Store can contain a lot of functions, maybe it's not good the pass an array of 1000 Stores objects... Am I wrong?
#4

[eluser]bretticus[/eluser]
I'm at a loss as to why you are storing Store objects in the first place. Can you explain what exactly a Store object is?
#5

[eluser]Rincewind[/eluser]
a Store object is an object which contains all Stores attributes like StoreName, StoreAddress, StorePhone etc... and relevant functions like AddStore, ShowAllStores etc...

I don't know if I should do it or not Undecided
#6

[eluser]Fenix[/eluser]
Just write your classes and store them in the library folder and instead of loading them with CI, just use require_once. I had the same problem trying to load it with CI but it's easy this way.
#7

[eluser]Harishkulkarni[/eluser]
I think you can use store class by loading it like $this->load->library('store');
and call functions inside class using $this->store->functin_name(); in your controller.
#8

[eluser]bretticus[/eluser]
[quote author="Rincewind" date="1239897256"]a Store object is an object which contains all Stores attributes like StoreName, StoreAddress, StorePhone etc... and relevant functions like AddStore, ShowAllStores etc...

I don't know if I should do it or not Undecided[/quote]

Just curious though why you have 1000 Store objects in a given instance? What does ShowAllStores do?
#9

[eluser]Rincewind[/eluser]
Well, to be honest, I exaggerated :roll: the ShowAll function I guess will return something like 10 Store objects at a time.
#10

[eluser]bretticus[/eluser]
[quote author="Rincewind" date="1239978859"]Well, to be honest, I exaggerated :roll: the ShowAll function I guess will return something like 10 Store objects at a time.[/quote]

Okay, sorry to be annoyingly inquisitive here. Let me try to explain what I'm getting at. Suppose you have the URL http://yoursite.tld/controller/action. As we know from the MVC model, controller is your class and action is the method in your class that does the work. Inside that action, you can load your very own library class. The problem is that to use the built in loader class to do so, restricts you to one instance of any given class. A class in this context typically provides some related functions that need to share common data amongst themselves like property variables, etc. I'm speculating here, but my "concern" is that perhaps your Store objects are really container classes (my terminology may not be standard here.) That is, you are storing information in them. That being said, a model is a more appropriate container in the MVC pattern IMO. In other words, do you really need 10 instances of your Store object in one pageload (inside one action or method?) If so, that's great. Someone suggested just using native PHP to create those instances. I'm fine with that. I can think of one potential need as an example. You may need to instantiate 10 database objects for gathering data from multiple databases or hosts. However, if these Stores, just hold data. You must initialize that data from something like a database, and if that's the case, a CI model can work like a Store object just fine. You can call the same model method as many times as you want with different parameters.

Just my 25 cents... :-)




Theme © iAndrew 2016 - Forum software by © MyBB