It is currently Thu Mar 28, 2024 6:52 pm



Reply to topic  [ 2 posts ] 
PHP OOP best practices 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post PHP OOP best practices
ok, so, this is mainly for Pig, but anyone else can jump in. :P

I'm playing with OOP (object oriented programming for you poor schmucks out there). I'm curious as to some best practices.

I'm building a user authentication class, mainly for code portability reasons. Any time I build user authentication into an app, is roughly the same. Minor improvements each time, of course, but blah.

Anyway, I was wondering...what's the best way to do that? Should I build everything right into the __construct method, or should I break it out across several public functions? Also, would I be better off just lumping everything inline into a single public function, or breaking the thing off into separate private functions? I'm guessing the second part is really more about ease-of-coding more than anything else. I just can't think of a reason for not sticking everything into the __construct method.

Here's what I've got so far for perusal. Maybe Gfree will have input too. It's not complete, but should be pretty obvious where I'm going. I'm just missing the actual authentication portion.

Code:
class userAuthentication {
   public $username;
   private $priv_password;
   private $result;
   private $authtable;
   private $dbtype;

   //constructor - set defaults
   public function __construct($username, $password, $db, $table){
      //clean inputs
      $this->username = clean($username);
      $this->priv_password = clean($password);
      //validate database type
      validate_db($db);
   }

   private function clean($variable){
      $pattern= array   (   '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
                     '@<[\/\!]*?[^<>]*?>@si',         // Strip out HTML tags
                         '@&(quot|#34);@i',               // Replace HTML entities '
                         '@&(amp|#38);@i',               // &
                         '@&(lt|#60);@i',               // <
                         '@&(gt|#62);@i',               // >
                         '@&(nbsp|#160);@i',               // nbsp;
                         '@&(iexcl|#161);@i',
                         '@&(cent|#162);@i',               // cent
                         '@&(pound|#163);@i',            // #
                         '@&(copy|#169);@i');            // copyright
      $variable = preg_replace($pattern," ",trim($variable));
      return $variable;
   }

   private function validate_db($db){
      //validate 'A' class dbs supported by ADOdb
      switch ($db){
         case 'db2':
         case 'vfp':
         case 'mssql':
         case 'mssqlpo':
         case 'mysql':
         case 'mysqlt':
         case 'maxsql':
         case 'oci8':
         case 'oci8po':
         case 'odbc':
         case 'case':
         case 'postgres':
         case 'postgres64':
         case 'postgres7':
         case 'posgres8':
            return $db;
            break;
         default:
            //invalid db == -1
            $this->$result=-1;
            break;
      }
   }
}


for the auth portion, I plan on checking $this->result, so if it's -1 (invalid db) it'll just die. Anyway, any input? Best practices?

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Tue Nov 01, 2005 10:45 am
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Aw man, I would've been able to give you OOP advice in the form of Java had i not dropped OOP this year. Im just not a programmer.

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Tue Nov 01, 2005 11:23 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.