My coding preferences:
I like to create classes, and put each class into their own file with the same name. I try not to make my classes to big, and not to small.
Im a period coder, which mean I program in periods and take breaks. Often I come back to programming with either same ideas or new ones, and I often try to reuse my old code. But I use alot of time to go through and change my code to fit my new idea, before I have started to touch my idea. Im now looking for a way to shorten this time. My question is both on code design, code practise, and organising of code, files and folders. I want to stress reuse, and less time used to fit the code.
Let us take the DB. Currently I use MySQL.myisam since this was/is easiest to create som mockup stuff. My current webhotell also support only myisam, but I want to port to innodb, and a few other just to learn to use them and future support. I currently use PEAR library since it make some stuff easy.
I have a db->connect() function in a single include file that returns the db_object. This db object is then passed as argument to other objects that need to do stuff with db.
I have one class for each table, and I keep all sql inside these table classes. I don't want ANY sql anywhere else in my application becuase I find it very annoying to go through every single line hunting for sql stuff that need to be changed all over the code. These table classes have basic CRUD functions like AddUser, EditUser, DeleteUser + special functions like GetUserBySessionID, GetUser( $user, $pass ), GetUserByUserID etc…
The rest of my application does not need to know anything about MySQL, SQL etc. Variables are stuffed into arrays, and then the arrays are passed to these table functions. I can in theory just make new table classes that support oracle, and the application wouldn't know the difference.
The good thing is that it makes it simple to create new tables, and functions. Very often it is copy/paste from other classes and modify them to fit. The application doesn't need any changes usually when I mess around with sql and stuff, and the other way around.
The bad thing is that Im unsure how well this scale with large number of tables, and relations, and how this would fit well if I went over to a relational db like oracle (which I want to try). Myisam isn't relational at all, and isn't even transactional which both innodb and the others are. I feel I lack a strategy for this matter.
I don't want to put every db functions into a single include file, becuase the file would be huge and easily messy. But it would be simpler for the application to deal with the access to the functions, and it would be easier to bring existing function over to a new application. But less OO and more mess in the code.
I was thinking of letting the application deal with data objects like.
Code: Select all
Class DB
{
$mydb
Connect()
{ /* connect and store a pointer to $this->mydb */
}
GetObject( $dataobject )
{
Switch( $dataobject )
Case 'users':
Return new User( $this->mydb );
}
}$user->set( $name, $password, $email ); /* then it would run the sql inside to create, or update a user */
Find user:
$user->get( array('password' => $password, 'username' => $username));
This would make the db class more usefull in the sence of having more than a simple connect function, but maybe it will be messy to maintain a large number of dataobjects? The rest of the application would also only relate to one class, and let the db class handle the rest, instead of the previous system where the application relate to table classes. This is a new thought in my head really, so Im trying to see if this would be a better or worse solution to what I have. What about relations? 1 user could be creator of many articles … should I then do $user->getUserArticels() … then be able to get an array of pageids? I think $page->getPages( $userid ), should I then be able to get the User object from the Page object? …. Or …. Im a bit confused here …
Wether I use table classes, or dataobjects:
Should each of these classes have the general functions, and then call database specific functions like:
tbl_user->AddUser() …. This one is merely passing the arguments to a tbl_mysqlmyisam_user->AddUser() which contain every mysql spesific commands? Then I could make an tbl_oracle_user->AddUser() with oracle spesific commands. Is this a good idea?
Organizing of objects in files and folders, and improve including/calling of those:
/root/
Constant.php - contains lots of various stuff like db info, tablenames, and other general variables.
Database.php - contain today the db_connect() function.
Page.php - contain general layout.
Index.php - main application. Creates an autenticate object, Creates a page, fill page with other form objects(loginform) and content. Basically glues everything together.
/root/css
Here is css files. Now they are php files that will need db access.
/root/app
Autenticate.php - deals with session, cookies, login/logout, uses db/table objects - but not dealing with any gui. No html here.
/root/app/tables
Tbl_user.php - require the db object. Deal with CRUD users. Contain sql for user table.
/root/forms
Login_form.php - creates the login html form, and have simple form processing. Need a pointer to Autenticate object for calling login().
/pear/
Pear files that are included wherever they are needed…
I find this system a bit messy. I find it messy to try to include files from all over the place, or pass objects all over the place. Every time I wanna try something new, I either have to keep my folder structure for not messing to much with the include paths. And I don't want to put all the files in one folder, which I find messy.