![]() |
[Tutorial] How to organize your classes | Part 1
How to organize your classes
Part 1: Database class + config files Section 1: Intro Hello and welcome to this guide! This will be a guide with 2 or possibly 3 parts. It will learn you how to organize your class files in a nice way, and how to write a good database class that gets your job done. Ofcourse this is only how I do it, and there are a thousand other ways, but I think you'll get a better understanding and possibly get some ideas from reading this guide. So, first we'll take a look at the structure and organizing of the files. For this to be easier to explain(and for you to understand), I will just pretend that we have a projectfolder called "test", located in our root directory. And since I do this on localhost, the folder I place my hosted files, is called "www". So what does this conclude? We have: Code:
wwwRight, so let's start the actual tutorial :P We want to have an includes folder, where we will have everything that we want to include. Code:
wwwSome folders that you want to have inside includes, is for example gfx, js, templates, classes, plugins Code:
wwwSection 2: The database class and the database interface Right. Alot of people use database to store things. Not only is it faster than if you would store it with.. say a .txt file, but it's also alot safer, but the main thing is that it's EASY to manipulate the data. This means that you could write neat admin panels to control your site layout, templates and lots and lots more!! But in order to get a nice way of handling the database, you need a proper database class, that will provide a set of functions for you to use inside your classes. This database class, will actually consist of 3 files. iDB.php - This is our interface for the class. DBmysql.php - This is the actual class for the database, in this case a mysql-database which I called mysql.php(convinient xD?) with the prefix of DB(DBmysql). DB.php - This is our.. DB factory, which makes it easier to include the database file. So let's start by taking a look at our interface! An interface is merely a blueprint on how the class should look, what functions it have to contain. This is neccessary, because when you guys figure out that you don't want to use a mysql database, but an oracle database, and you write your own db class(DBoracle.php), you will then see if your class follows the blueprint. The interface is define with PHP Code:
So what functions should the database have? Well, first of all, we need a function that will set the values of the hostname, username, userpass and dbname. We would also need a function that connects.. etc etc. I'm sure you can figure out by yourself all the functions that a database class should have. This is my interface anyhow: PHP Code:
So the next file, is ofcourse the database class itself. As I mentioned before, this is just the mysql since it's most used. PHP Code:
And ofcourse, we have to have all the functions that we defined in the interface. PHP Code:
The end: PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
The explenation comes later.. ;) The whole class: PHP Code:
This makes it really sweet for you when you write your plugins later :) Section 3: The database factory So onto the DB factory! This is the.. ultimate thing. It makes it so easy to include the classes it's unbelievable.. :P We name the file DB.php and the first thing we have in it is an autoload function.. PHP Code:
PHP Code:
Secondly, we will have the factory class: PHP Code:
Static??!?! Yah! It's something awesome, and sweet, and.. completely madness :D Static means that you can use it, without instance the DB class. Instead of writing: PHP Code:
PHP Code:
So what does this getInstance do? Well, first we check if the variable $DB is an object already, and if it's not, we do the things inside the bracket. Also note, that when dealing with static variables or functions, we have to use self:: instead of $this. Actually, I think this function is pretty easy to understand. What happens is that you include DB.php Then: PHP Code:
Now don't tell me that this ain't neat? :D NOTE: iDB.php, DB.php and DBmysql.php should be placed in the classes folder! Section 4: The config and classes files So the configuration file will ofcourse contain alot more when you have your plugins written, but right now we'll just include the basic stuff. This is my configuration file: PHP Code:
It's the $tanaxia variable(in this case for me it means that it's belonging to the core), and it's the ['config'] array, which means for me that it's a setting for the core. The next array ['database'], means that it's a database setting, and then the next is defining what setting. I then start the session, and include classes.php Onto the classes file; this is what makes or breaks the script. Here we start all the classes, AND we use the setCol and setTable functions of the database class. This is the classes file: PHP Code:
Then we create the database, based on the type settings in our config file. After that we set the handler with all the data provided from the config file, and then directly we chain the next function(remember the chain I talked about?) and connect to the host, and then select the database. That's really everything we've done now. The things that are commented describes how to use the setTable. The good things with that, is that later when you use the db object in the plugins(for example news plugin), you want to insert values into different columns in the database. An example would be this: PHP Code:
Then you need to enter your class and change EVERYTHING to match the new name. And that kinda stinks. So let's say you change the news table name to "articles". Then you just enter your classes.php file, and change this line: PHP Code:
PHP Code:
NOTE: Both config.php and classes.php goes into your includes folder! So the structure we have, looks like this right now: Code:
www |
Anyone? >.<
|
Interesting tutorial, personally I use the phpdoc comments at the start of a file e.g.
PHP Code:
I'll be interested to see your plugins tutorial as I'm struggling myself with a similar project. |
Quote:
Indeed, if they want to write an oracle db, or mysqli or w.e.. Thanks I'll write it whenever I see more interest xD :-) |
I've not read thoroughly through the entire article (time is money) but I do however have a few comments to offer up. Firstly, the filesystem structure itself; why is there a folder named includes when some things in there aren't simple PHP files to be included into others (e.g. the js folder)? A more suitable name would suffice, or indeed a complete separation of groups of files serving different purposes (javascript, css might be better suited outwith the PHP files' folders).
A next point, and one I won't labour too much, is the way you have written the various DB-related classes. To me at least, whether you did this knowingly or not, it all seems very MySQL-centric in how things are done. You could certainly do with working with a wider group of databases and seeing which common methods are really needed, and those which are more tailored to working with MySQL in particular. Next, your autoloading and factory method. Why have both? Why separate the two? It's not my point to provide answers right now but it's something to consider. I will, however, note that you can only define a single __autoload function. It might (read: will) be easier to use the SPL library (e.g. spl_autoload_register) to keep the DB autoloading separate from any other autoloading which might be needed elsewhere in your project. For the configuration file, I'd like to see things simplified much more. Have separate config files for different jobs. For example, one for the MySQL database settings, another for SQLite database settings, yet another for caching settings (if that were to be part of your application), etc. Keep the variable names within those files simple (just plain $config works fine) and make use of the include-scope when loading/using the configurations (if that's over your head, I'll explain at a later date).Anyhow, there are some comments to tide you over until I can really sink my teeth into this come september! :) |
Quote:
Code:
<link charset="utf-8" title="no title" media="screen" type="text/css" href="includes/gfx/layout.css" rel="stylesheet">Code:
<script charset="utf-8" type="text/javascript" src="includes/js/mootools.js"></script>Quote:
Quote:
And within classes.php, you always include DB.php, thus I've placed it there. However, I could place the autoload function in classes.php instead if you like that, but it doesn't really matter the way I see it if it's in DB.php or in classes.php. But I'll take a look at spl autoload! [quote=Salathe;17443] For the configuration file, I'd like to see things simplified much more. Have separate config files for different jobs. For example, one for the MySQL database settings, another for SQLite database settings, yet another for caching settings (if that were to be part of your application), etc. Keep the variable names within those files simple (just plain $config works fine) and make use of the include-scope when loading/using the configurations (if that's over your head, I'll explain at a later date).[quote] This is all a matter of preferences ofcourse. But you are all free to create your own variable names for your configuration file. I'm just mearly showing that you should have a variable for the dbhost, one for dbuser, etc. The thing about several configuration files, will actually make it harder, at least to me. Why not keep it all placed in one file, which will make it alot easier to find and read the config. Instead of having to go through 10 configuration files just to find the setting that you're looking for if you're not sure where the setting is. And about the seperate DBsettings. You should only have 1 database, so thus you can easially change that by changing the variable that contains the value for "dbtype" from "mysql" to "whateveryouwant". Quote:
|
I'm very grateful for this tutorial. It helps me very much.
|
I've just written a MySQL class too few days ago. It actually resembles your class quite much. I'll post it later this week :)
Nice tut btw ;) |
Thanks for goood tutorial. This might also an old tut of you.
But it was help and the comments Salathe as well. |
Hehe wow, people start commenting all the old tutorials I've posted xD
But I'm glad you liked it, and that it was helpful :-) |
I searched the Part II. I think it does not exist. I hope you get time to finish the second part too.
|
| All times are GMT. The time now is 09:14 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0