Jump to content

Class file structure and calling method


MrAdam

Recommended Posts

Hi guys,I have a quick question regarding class structure and the method you'd use to call them. I'm writing a simple framework for a project I'm working on, and the main framework directory has 2 sub-directories; "procedures/" and "library/". The library contains classes such as DB, Config, Session, Page, etc. Then the procedures directory contains classes which contain sets of methods for performing specific DB queries; Account::getUser(...), Messenger::getInbox(..), etc.Currently the include path is set to the framework directory, and an __autoload() function handles including the right library or procedure. What I'm trying to do though is avoid any ambiguity when instantiating the classes. For example if I had a library and a procedure with the same name, obviously best avoided anyway, but the first one found would be included. Also I'd like to reduce the number of file_exists() checks where possible.I was considering a Zend naming approach with underscores indicating a new directory, and then instantiating the classes like:

$db = new Library_DB();

Which would result in "library/db.php" - but I'm not sure I'm keen on having to specify "Library_".What are your thoughts for something like this? How would you do it?Thanks for any help,Adam

Link to comment
Share on other sites

I'm not sure how this zend thing works but it seems unstable if it's turning every _ into a /. And calling this

$db = new Library_DB();

is going to try and construct the Library_DB class. So your DB class in Library must be Library_DB. If you have classes in multiple directories you are going to need to specify where they exist somewhere. Logically. Unless you have a script that checks all directories for the class name specified. And you'l need a file_exists every time you need to catch a possible user input error but being the designer you can probably think of a way to do this without.But I really have no idea what your question is. I'm trying to determine that and hopefully I got it.The way I do it is just put all my Classes in a "classes" folder and include them at the beginning of pages I need them. I haven't tried autoload but as long as the classes do not change directories you should be able to locate from any location. If the location changes I would make a script that checks how many directories you are from the www directory, convert that number into ".../"s and then once you've back-peddled into the www directory then move into the known class folders.

Link to comment
Share on other sites

Sorry MrFish, don't think you quite understood what I meant.__autload() provides a way to automatically include the class file without having to manually include it at the top of each file. When you instantiate the class, if it doesn't already exist, __autoload() is passed the name of the class and you're able to try and include it with the logic in that function. If it fails, then an error is thrown as usual. Combined with multiple include paths you only have to pass the class name and you can look within several directories for the file before throwing an error if unable to find it.Replacing the underscore with a slash is a common way to indicate a new directory, just with my example it doesn't look very nice. Zend use it in a much more sophisticated way, which is why I'm asking how others would approach my situation. I'm tempted with using several registered spl_autoload functions instead, to remove the file_exists() checks, but as I said looking to hear how others would approach this.I don't want to store every class within a single directory because the project is going to grow and I want a more efficient, meaningful file structure.Thanks for you time anyway.

Link to comment
Share on other sites

The underscore naming scheme should work fine, you need some way to either remove ambiguity or just have it not work with things that share the same name. Specifying the directory is really the only way to remove ambiguity, and underscores are a logical way of doing that.Other than that, it's hard to say much about optimizing file_exists or whatever else without seeing what you're working with.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...