Welcome Guest, Not a member yet? Register   Sign In
General library question
#1

[eluser]xadio[/eluser]
Right now I am working on, what I would call, a series of libraries. These libraries are really classes that are working together, in the background, to aid my controllers. So following are some of the general questions I have.

1) Is it recommended to place all classes into libraries or should I create a separate directory and require_once()?

2) The following line $this->load->library(); initializes the library when called. This defeats the purpose of the library I am designing which happens to be a Singleton. Would you recommend initializing the library by hand or modifying the Loader library to handle design patterns?

3) Is it proper to have libraries require each other or do they need to encapsulate multiple classes? For instance I have a class Animal and a child Cat, do they need to be in the library Animals or in separate libraries Animal and Cat, respectively?

4) Is it preferred to keep helpers as a series of functions or can they be classes?

I guess where I am really confused is what to do when I want CI to use my classes, yet remaining within the framework and using the benefits it offers.

Thanks for your thoughts!
#2

[eluser]tomcode[/eluser]
I am not very familiar with the terms You use. CI allows You to work the way You approve it. If You want to stick to commonly (basic) used techniques :

-1. keep it simple
0. read the User Guide
1. 1 class = 1 file
2. no sub folders in the class folders (librairies, models, hooks)
3. don't alter the system and it's libraries ( as a starter )


Since I work with CI, I almost never use include() & CO.
Helpers are globally available functions (also inside the classes)

I'm not familiar with the term Singleton, but CI instanciates the loaded classes only once, isn't that a Singleton ?

Animal / Cat

file Animal.php:
Code:
class Animal{

  function Animal(){

    //..

  }

}

file Cat.php:
Code:
class Cat extends Animal{

  function Cat(){

    parent::Animal();

  }

}

I recommend You to play with CI, download some of the excellent contributions available and look how they work.
#3

[eluser]elvix[/eluser]
i agree with tomcode, plus grab some sample code -- FreakAuth Light is pretty comprehensive (models, libraries, helpers, all working together), so I might look at that as a reference.

You can autoload libraries via config/autoload.php, maybe that's helpful?
#4

[eluser]Pascal Kriete[/eluser]
Ok, for most of the stuff the answer is - do it the way YOU want to do it. Helpers, by definition, are groups of functions - but I often use them to create one word accessor methods for my libraries (i.e. check_Login() instead of $this->auth->check_Login()).

CI already provides handling for singletons. All of the CI base libraries are handled as singletons, called with the load_class function, found in system/codeigniter/Common.php . It will look through your application/libraries folder, so that would be a good place for it.
#5

[eluser]wiredesignz[/eluser]
CI plugins can be class files also but they are NOT instantiated by CI when loaded. (unlike Libraries) You can instantiate them as needed.
#6

[eluser]xadio[/eluser]
First thank you all for the extremely fast responses... wow!

@tomcode: A singleton is a class that has a private constructor and a public method which instantiates it, you may be right, but I will have to think about it. In regards to your points, (-1) wish I could, (0) done, (1) sounds good, (2) Why? I like to organize my code and that would just make everything convoluted to me, (3) I agree. My question was geared more towards where would you put Animal.php and Cat.php?

@elvix: Thanks, I have been using FreakAuth as a template, but there are some things that are not to my liking.

@inparo: That's good to know, I will look into it and see if it does what I require. The only problem I have is keeping all my code in the root of libraries.

@wiredesignz: Maybe a plugin is what I am really looking for, but the UserGuide says "Plugins work almost identically to Helpers. The main difference is that a plugin usually provides a single function, whereas a Helper is usually a collection of functions." That quote seems silly to me, but I will see what I can do. Thanks again for the help.

Thanks for all the help!
#7

[eluser]tomcode[/eluser]
Quote:(2) Why? I like to organize my code

Yeah, me too, but Ci does not offer this (folder application/libraries) so I work with pre_/sub_fixes if neccesary, but since I rarely use more then ten libraries ....

That is, You can organize Your MODELS: http://ellislab.com/codeigniter/user-gui...odels.html.

Quote:My question was geared more towards where would you put Animal.php and Cat.php?

http://ellislab.com/codeigniter/user-gui...aries.html

application/libraries/Animal.php
application/libraries/Cat.php

or if I want to simulate a namespace

application/libraries/Tom_animal.php
application/libraries/Tom_cat.php

Do a forum search for Matchbox, You might like that, too complicated to my likes, I prefer stuff like ErkanaAuth (another forum search...)
#8

[eluser]xadio[/eluser]
Quote:application/libraries/Animal.php
application/libraries/Cat.php

or if I want to simulate a namespace

application/libraries/Tom_animal.php
application/libraries/Tom_cat.php

That's what I thought, thanks!

Quote:Yeah, me too, but Ci does not offer this (folder application/libraries)...
Do a forum search for Matchbox, You might like that, too complicated to my likes, I prefer stuff like ErkanaAuth (another forum search...)
Hm, yea matchbox is nice, but I don't need to handle modules, yet. ErkanaAuth doesn't leave room for reuseability without major modification. I guess for what I am doing I will need to extend the CI core. For instance it would make sense that if the library requested was 'factories/Factory' one would look in 'app/libraries/factories/Factory.php' as a file cannot have a '/'. Do you know of any reason this isn't handled?
#9

[eluser]tomcode[/eluser]
Don't have the faintest idea and no time to look into the code right now. But, how many libraries do You want to handle in one application ?
CI is mainly built for simplicity and speed. Frankly, even if You have thirty ! libraries, with a litte care on the naming You should get things straightend out, instead of breaking the core code.

Still, if that's You want, You can extend and overload allmost all core classes, but I do this for cosmetic changes, CI provides me with what I need.

http://ellislab.com/codeigniter/user-gui...asses.html

Most of the times it's my lack of knowledge of it's possiblities that stop me.

You might post a new topic with the concret problem, there are for sure some guys who'd love to help You out with this.
#10

[eluser]zdknudsen[/eluser]
Yes, it would indeed be easier to help you if you presented us with the concete problem at hand. Smile

On a side note, if you are looking for better organization possibilites, you should reconsider Matchbox. It's pratically just that... Better organization.




Theme © iAndrew 2016 - Forum software by © MyBB