Welcome Guest, Not a member yet? Register   Sign In
Singleton issues
#1

[eluser]jwindhorst[/eluser]
Is it possible to break out of the singleton pattern inside of CI?

I would like to have an array of contact objects, each of which would have getters and setters for the private variables. I've created my contacts class, and on the surface it appears to work just fine. Once I set the values for array[1] (contactObject) it alters the values for all objects in each element of the array.

I suspect this has to do with the Singleton pattern that seems to run rampany through CI, but it would make my code a lot cleaner if there was a way to make this work.

Thanks
#2

[eluser]narkaT[/eluser]
you could take a look on datamapper or ignited record Wink
#3

[eluser]jwindhorst[/eluser]
Thanks narkaT but I don't want to map my objects directly to the database. Yes, of course there is a reason for this.

In the first post I mentioned contacts. Contacts 'might' not be in our system as users, but they might be. Therefore, I have a contacts_map table that figures out if they are just a linked in user, from the system, or if their information lives in the contacts table only.

That being said, I can set the "contact_type" attribute on the contact object based on which table they were pulled from. But not in a singleton pattern of course, because it would appear that all contacts get the attributes of the last one that I set.

Very frustrating.
#4

[eluser]narkaT[/eluser]
ah okay.

how are you instancing your objects?
That's a interesting issue Wink
#5

[eluser]jwindhorst[/eluser]
It doesn't seem "interesting" to me really, fairly standard outside of CI. At any rate, I created the class in the library directory and then auto-loaded the library. (This is likely the root of the problem) That being said, I can simply use/instiate the class (it likely is instantiated when the library loads really) with the following code
Code:
$this->ahcontact->setAll($arrOfValuesToSet);

I suppose explaining this in more detail has simplified the question. The real question now becomes, can I use the "new" constructor on classes in order to instantiate them from within a CI controller?

Does that make sense? I could post more code, but I have a deadline tomorrow, and at least for now, am working around the issue with associative arrays of data (not as clean as I would like however.)

Thanks
#6

[eluser]narkaT[/eluser]
[quote author="jwindhorst" date="1229043784"]The real question now becomes, can I use the "new" constructor on classes in order to instantiate them from within a CI controller?[/quote]

yes you should be able to instantiate multiple objects from the class Smile
#7

[eluser]jwindhorst[/eluser]
Hah! Ok, so this makes much more sense now. I was almost ready to throw code igniter out the window with the baby and the bathwater! Now however, I see what is going. Using the new constructor did indeed allow me to create the multiple objects I was looking for!

Now I'm happily pulling out the placeholder associative arrays I had in place, and moving forward with the objects I was going for in the first place.

Thanks again narkaT, it's funny how sometimes someone else asking you a question can help you find the answer even if they don't tell you.

Good stuff, happy hacking!




Theme © iAndrew 2016 - Forum software by © MyBB