Welcome Guest, Not a member yet? Register   Sign In
Best Practice for instantiating classes in Libraries that are non-singletons?
#1

[eluser]glopv2[/eluser]
Hi all!

First, I am a big fan of CI. What a great project! And expertly done. So much so, that this is the first time I've ever felt the need to post and it's more of a best practices question than a "how do I do this?" question (disclaimer: I'm not great with PHP). Unfortunately, I couldn't find a relevant answer on the forums searching. Okay, here goes:

- - - - - - - - -

I want to push data around on my site using objects and classes. Let's say I have this:
Code:
class person {
  public $name;
  public $age;
  
  public function __construct($newName, $newAge) {
    $name = $newName;
    $age = $newAge;
  }
}
I want this to be the format that some models return data, and the format in which some views expect data.

My question is where do I store the code that defines the class?

- - - - - - - - -

1. The first obvious answer is as a library, but the problem is a library seems to only work for the singleton pattern. I want lots of persons, each one with unique data. I don't want to attach one instance to the $CI object. I just want my whole application to be on the same page about what a "person" object is when it gets passed around.

2. The second answer was a helper file, but the problem here is that the (excellent) documentation indicates that helper files are really for simple scripts and functions. Not for classes.

3. The third answer was to use a require_once() call and make a new directory for these classes. This seems bad to me, because it lives outside of the framework in a sad lonely island by itself. I want to adhere to the principle of least surprise, and seemingly random require_once() calls may be confusing to every developer but me.


So, having suggested and shot down those 3 ideas, can anyone tell me which one they think is best, or what is a great 4th alternative?



Thanks!


Messages In This Thread
Best Practice for instantiating classes in Libraries that are non-singletons? - by El Forum - 06-11-2010, 04:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB