General library question |
[eluser]xadio[/eluser]
[quote author="tomcode"] 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. [/quote] That isn't really the point. If one has coding standards shouldn't he stick to them? [quote author="Zacharias Knudsen" date="1202341068"]Yes, it would indeed be easier to help you if you presented us with the concete problem at hand. ![]() On a side note, if you are looking for better organization possibilites, you should reconsider Matchbox. It's pratically just that... Better organization.[/quote] I will get back to you on my concrete problem, but first I need the UML verified by my mentor. I will reconsider Matchbox, but following is what I threw together in 5 min. Code: //MY_Loader.php Do these three lines really make things that much slower?
[eluser]tomcode[/eluser]
Quote:Do these three lines really make things that much slower?For sure not. Go ahead. Try it.
[eluser]xadio[/eluser]
@tomcode: Yea no significant speed decreases, but I haven't run a full test. Thanks for all your help tomcode! @Zacharias: I have decided not submit my project yet because it is still in design and I will get flamed when people know what it is. The pieces that I am working on have to deal with (GoF) (Abstract) Factory and (GoF) Singleton Design Patterns. Code: (abstract) class Factory { So as you can see Factory is essentially a Singleton or abstract, and probably will never need to be called. However, I need to require this because load->library will initialize the instance. SomeFactory is a (concrete) factory producing some concrete products depending on how it is initialized. So CI works well, but it puts a damper on some of what I want/need.
[eluser]tonanbarbarian[/eluser]
Ok here are a few of my thoughts on this 1. I have nothing wrong with using your own coding structure, but if I wanted to release my code I would make it fit more with CI. That doesn't exclude what you are doing though, there are ways around it 2. Having fiddled with the loader class and attempting to allow loading libraries from sub folders i fould the easiest way to do things is create a seperate library that loads my sub libraries so for example you might create a factory library that existed in the application/libraries folder like all good libraries do, then use the factory library to load the files necessary from the sub folders Code: $this->load->library('factory'); I have used something similar in a code library I hope to release one day. 3. Finally I know this will started a heated debate, but you are using PHP5+ specific code by having abstract classes etc. If you release this code library to someone else it will not work on PHP 4 like the rest of the CI core does. If you never plan to release it then this is not an issue.
[eluser]tomcode[/eluser]
tonanbarbarian was faster ... So well, You found Your luck. For convenience You could extend the loader class : libraries/MY_Loader.php which will be loaded automatically. Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
[eluser]xadio[/eluser]
Thanks tonanbarbarian I like your idea about creating a library which handles the loading it is probably a better way. Yea I have decided to make the permanent switch to PHP5, so no going back now. @tomcode: I have done that so far, but I like the idea about singleton_load. I will have to really think about my alternatives. I really like CI over Cake and so I will just work within the boundaries. Not to stray this conversation to another topic, but what is the dev cycle for CI? Do they take suggestions like the one mentioned above or do they monitor the forums and take what they like?
[eluser]stevezissou[/eluser]
The singleton pattern is used when you need a single instance of an object application wide ... in other words the object gets instantiated only once and this one instance can be accessed whenever you need it. The standard example always used is of a logging application. Is this what you are really trying to do...not the logging part but needing a single instance throughout your application? As for the libraries/classes issue, I am having a similar organizational issue myself and just placing everything into the library folder isn't going to cut it after a while...especially if you are doing a lot of object-oriented things...like encapsulation. I don't necessarily see a problem with using require_once() in a similar fashion to the way you would utilize import statements in Java or Python. But if you think of a clever alternative, please post it as I would be interested in hearing your solution too.
[eluser]xadio[/eluser]
@stevezissou: Yea, you are right and essentially that is what CI emulates, but I am designing a Factory (possibly abstract), and I don't want any instance of it. On the other hand I might want one Factory instance, but if I did then I want to be able to do Factory::get_instance(); for consistency. The best I could do with the organization is a MY_Loader class which I mentioned above. The other alternative would to create an CI_AdvancedLoader which would allow for better handling of PHP5 abstract, public/protected, and interface keywords. I also submitted a thread under Feature Requests to use variables for 'libraries/' and 'config/' which would be initialized in the constructor. More detail here: http://ellislab.com/forums/viewthread/71028/ Can I call you Stevesy?
[eluser]stevezissou[/eluser]
Mmm, I'll check out your advanced loader idea. For me, I just create a separate source folder and follow a similar file/folder convention to Java and store all my logic and objects that are clearly separate from controller logic in this source folder. (Within the source folder I am also creating sub-folders too for everything.) Then I can "import" any class I need and use it in a standard object oriented manner. With this practice, I can make abstract classes and interfaces easily, use design patterns and just keep everything nice and clean. I also create a common constant file where I store common variables like MySQL statement strings and things of that nature (so I have nothing hardcoded anywhere). This way I have a clear and clean separation of all things. If you are using PHP5, PHP even extends this practice by giving you autoload support now. -Can you call me Stevesy? Hahaha...only if you can locate a pair of Steve Zissou adidas.
[eluser]xadio[/eluser]
I have been working on this so called "Advanced Loader", AL, however I am designing it for PHP4 so it can be used by all. I want to be able to dynamically make my factory products as well as fix the issues with CI's support for PHP5. Maybe I am going crazy or over the top, but I want this system to be as autonomous as possible. Right now it works in sync with CI_Loader as I want to allow the user to use either loader at the same time. So far the AL will parse the requested library, using a preg_match and extracting extends and implements which it will then scour (customPath, currentPath, APPPATH.$this->libraryPath, and BASEPATH.'libraries/') for the requested classes and interfaces. It checks the Loader::_ci_classes / get_included_class() / get_included_files() to see if they have already been included else includes them. There are some things I still need to work out, but if you have some ideas please feel free to post. (ie I forgot about abstract class and need to readjust my regular expression to handle it.) I can post the current development, but I am still working out some issues (ie eliminate infinite loops by recognizing this) Code: class Class1 extends Class2 {} Some issues: 1) How to handle including a file with AL then needing it initialized with CI_L? 2) Which directories to search? How to handle each user's unique organizational patterns? 3) Should this completely take over CI_Loader? 4) Should this have another option of parsing a XML, configs, or (C, C++, Java) include style instead of parsing the class line? |
Welcome Guest, Not a member yet? Register Sign In |