note 40765 deleted from ref.classobj by nlopess

From: Date: Sun, 14 Mar 2004 10:25:49 +0000
Subject: note 40765 deleted from ref.classobj by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-66811@lists.php.net to get a copy of this message
Note Submitter: Fasoro Oladipo <dipofasoro@yahoo.co.uk> ---- <?php /*Since PHP is object oriented OOP. Use it's features to the maximum. You can define ur business logic, data logic encapsulated in classes. For people who are not OOP,you might be wondering why this is needed. it is for code clarity, reusability, portability, ease of maintenace. Let's say for example, your site does a Login, Register, Feedback, Update, UnRegister and u are using a Informix database server. You can */ //file user.php class user { var $dbconn function user() { //do initialisation $this->dbconn = DB::getInformix(); } function Login($uname, $pass) { //do login on uname and pass on Informix Server //run SQL queries on the Server } function Register($uname, $pass, $name, $email) { //do register by running SQL Queries on Informix Server } function FeedBack($name, $email, $msg) { //add feedback to informix server by running sql_queries } function Update($name, $email) { //update name, email on Informix Server } function UnRegister() { //delete user account from the informix server } } //file template.php class template { function showlogin() { //show the login interface } function showwelcome() { //show welcoime when the person is logged in } } //file DBs.php class DB { function getInformix() { //get informix database conn } function getMySQL() { //get MySQL conn } function getPostGRESQL() { //get PostGRESQL } } /* the file user will be included in all pages that can do a login or reg. To get an informix database connection call DB::getInformix(); all username, server, password everything is handled in there. Now the cue is to do login you just*/ $usr = new user(); $usr->Login($_POST["uname"], $_POST["pass"]); //to do a register the same too is applied. $usr->Register($_POST["uname"], $_POST["pass"], $_POST["name"], $_POST["email"]); /* and so on.. all the logic of validation ands inserrting into the database is (login/register) is done behind the bars of the class to show the login interface you call template::showLogin() You might have been wondering then what is new. Consider if you want to change the login interface and the login has been declared in 20 pages differently, it will be hard to add a [Forgot Password] link at the bottom of the submit button. but if it were to be declared as a class function just change the look in template::showLogin() and it will reflect in the 20 pages immdeiately. Consider again that you want users to specify that if they try to login from that particular system IP address. They should be automatically logged in without showing login interface. You can just extend the function to add an extra parameter */ Login($uname, $pass, $rememberIP); /*and add a checkbox to template::showLogin(); call */ $usr->Login($_POST["uname"], $_POST["pass"], isset($_POST["remeberIP"]); /* and in the function interface you change your business logic to accomodate the new parameter to taste. Now let's say you just assume you connect to the informix database in 35 places in your site and you want to change the login password to database. just chasnge it in DB::getInformix(); and it will be reflected in the 35 pages if all of them call DB::getInformix(). This will have saved u a headache of Search/Replace in 35 pages if you have connected explicitly in all the pages. Another main issue is your IT manager comes the next morning and tells you that he has heard that MySQL is now the bomb and wants a Database server change to mySQL without the site getting down. if you are the wise one like me just change $this->dbconn in the user class to DB::getMySQL(); in the constructor. if u are not as wise. i am very sure this will give you at least a week long HeadAche. [Why Not Use Classes] is an presented by Fasoro Oladipo, in Nigeria. Fasoro Oladipo is a staunch advocate of OOP. Thanks for reading this text, hope you have a change of mind. */ ?>

« previous php.notes (#66811) next »