![]() |
OOP - User System
Greetings!
In the past 2 days, I am just reading the basics of OO-PHP for i find it good and comfortable to use in doing stuff than the old-fashioned way. And just this day (thanks God), I have done lots of test and sample codes about the basics on how the OO-PHP goes around and somehow got the basic/simple concepts about it. In regards to the new concept I have learned/understand, I wanted to do something out of it. I have decided to create an OO based User System (a basic/secured one). Its a user system that can be able to login and register users with no any other etc. features yet. What on my mind right now, is to create pages likely like these below: Code:
index.php #main pageThanks, t3st |
The first thing I would do, is to create a DB class that manages all the querys and all that.
After you got your DB class, you would begin creating a config file, where you start all your classes, so you create for example $db = new yourdbclass('params'); Then you create a index.php, include your config, and run it. If it displays blank, then you're good to go. Start creating your user class. There's alot of ways to do a user class, but I would generally do functions in the order they're used: function to - check if username and password is correct - login the user - logout the user - register a user - etc There's no real "guidelines" on how to do this. But just take it easy and whenever you hit a problem, systematicly errorcheck your script on what could be wrong. Good luck! |
Thank you about it Tanax, it really enlighten me a bit on how should i start this so-called project that i will be doing.
Any comments/suggestions are more welcome :) |
I just thought of something else, that you would take great advantage of.
When you create your user class, in your construct, or in a setdb function, set a DB variable, something like this: PHP Code:
PHP Code:
Anyways, what this does is that you can use the DB functions inside your user class, so whenever you query, you just do: PHP Code:
|
great!, thats pretty useful, im having a great readings today coz i wonder what will be the best thing to implement for me to set standards though.
Should i be having an interface in a class? |
I would say no. Not really neccessary for you to build an interface. But you can do whatever you want. If I put it like this: It won't really be any difference right NOW if you implement an interface, or not.
|
So far, this are still the things i have done. My codes are below, and any critiques about it are very much welcome for me to improve.
sample_db.php PHP Code:
PHP Code:
PHP Code:
Feedback? Thnx. |
Updates:
sample_db.php PHP Code:
index.php PHP Code:
Code:
nothing changeIm still messing around :) any feedback is greatly appreciated. Thnx. |
var is depreciated in PHP5, use public/private/protected and if your testing on a host they oft won't let tables be created via scripts.
|
You don't set $this->dbquery anywhere.
You haven't even defined $dbquery anywhere. You defined $query, which isn't used anywhere. You used var $query, instead of private $query, which would be the most optimal choice. When you disconnect, you disconnect $this->connection, which isn't set anywhere either, NOR defined. You return your query results, even if it failed. And lastly, I don't think you should echo ANYTHING AT ALL in your class. Classes are just a way of managing your data. Return DATA instead, and process the data in your controller file(in this case - index.php). PHP Code:
|
Drop the "var" keyword. It's deprecated as of PHP 5.0.0.
Now, about the problem with the table creation. Tables are assigned to databases. Therefore, you need to actually select a database before trying to create a table. In your do_connect method, after mysql_connect, you need to add another line: PHP Code:
Also, try to add class properties (..or class variables, as some would call them) as you need them, not randomly across the code. And please, oh please, stop suppressing errors using the error suppression operator (@). Use error_reporting(E_ALL) combined with ini_set('display_errors', 'on') when debugging, and the opposite when going live. You MIGHT need to debug some problem later and you will not be able to tell what caused that problem, if you suppress all the errors/notices/warnings, what ever. Enfernikus: creating tables cannot be enforced on that level. You can, or you can't create tables. End of story. It doesn't matter if you run a query in phpmyadmin or if you run your own php file that creates it. As long as a "create table" privilege is given on the user you're using to connect to the database, creating tables can be done from anywhere on that server (and in some cases, remotely - where the server accepts remote connections - not really the case with mysql). |
o.O thanks for the replies, i just notice that im pretty focused on something, that i didn't notice im did create a table though what im thinking is that im planning to create a sample db. whew!
update code will be posted later, i need to modify it now. thank you. |
Yes, i have done updating my codes, and i have choose to use an interface for my class.
db.php - interface PHP Code:
PHP Code:
PHP Code:
PHP Code:
Any more comments and suggestions are welcome to help me visualize and improvise which is really better for usability of codes :) Regards, t3st |
Mm better :-)
You could go for a fetch function which would fetch an array? Also you could design a secure function which would secure the specified string from mysql injections, and then return the secured string? Just some from the top of my head! |
Thanks Tanax, im adding it up to the list of functions that i will be needing for the project.
PHP Code:
All the best, t3st |
Yes you can use the same variable name on all functions.
However, there already is a function called "stripslashes" for example, and you cannot use the same name as an already existing function. I would suggest you to "merge" the 2 functions to a function called something like "secure". I also thought about something in your updated code. You don't actually check if the connection succeeded, so I would suggest you to do something like PHP Code:
|
So thats it, thank you very much Tanax :) I really appreciate it and you guyz really help me a lot. I got a better understanding now in OO than the last day :)
Time for my hands to take on some notepad++ :) |
Glad to help!
Just post if you need more help :-) |
Quote:
|
Thanks bud :), you have a point there, but im testing it locally for the moment. Learning purposes only :)
|
| All times are GMT. The time now is 08:57 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0