Jump to content

adamlang22

New Members
  • Posts

    4
  • Joined

  • Last visited

adamlang22's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Sorry for the late reply, I really appreciate your input. I'm going to go and learn a lot more about dependency injection and the stuff about testing made sense. I need to start doing unit testing to move towards TDD which I think will present problems with my existing code and force me to improve my programming.
  2. And for some reason on my Db class I wrote method instead of function?????
  3. Okay sorry I don't think I explained myself properly. I just wanted to make sure this was a good way that other classes could use the database rather than for example injecting the database object into a new class. And the fact I'm using static methods which I don't see too many people using and they instead instantiate the database class.
  4. I have been using the below method to access my database for some time now and just wanted to make sure im doing the correct thing. My application runs perfectly but im just trying to improve my programming skills. The classes are all loaded in with an autoloader so in theory using the classes below I could just call: echo User::getUsername(1); Like I say it works fine I would just love some feedback about what other people do and suggestions on something that might run better or looks cleaner. class Db { private static $db_read; private static $db_write; public static method read(){ if( self::$db_read == null ){ //create new database connection is it doesnt exist self::$db_read = new PDO(); } return self::$db_read; } public static method write() { if( self::$db_write == null ){ //create new database connection is it doesnt exist self::$db_write = new PDO(); } return self::$db_write; } class User { public static function getUsername($user_id){ $d = Db::read()->prepare('select * from user where id = ? '); $d->execute( array($user_id) ); $user = $d->fetch(); return $user['username']; } }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.