scottybwoy Posted November 19, 2008 Share Posted November 19, 2008 Hello you helpful lot, I saw this framework tutorial a while back but never gave it time. It looks like a really good place to start, but I wanted this to be my second php5 project, possibly first php6. Firstly does anybody know a similar tutorial using php5/6. Secondly I didn't want to use PEAR but make my own slimline database abstraction layer. I don't mind using this one as a tutorial but I'm having trouble converting PEAR::DB into mysqli::db. I have created a file mysqli.php with a class named db that has a constructor that connects to the database and looks like this : class db extends SystemBase { function db() { $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT); return $mysqli; } } I have edited the general function &db_connect() to look like this: function &db_connect() { require_once 'mysqli.php'; new db; return $db; } I think it is working as it poses no errors, and I just wanted to know if I'm going the right way about it? Any ideas/views from someone a bit more experienced? Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 24, 2008 Share Posted November 24, 2008 I thought I already answered to this... You're using PHP4 object syntax. If you're so eager to move to PHP5, use PHP5 syntax. Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted November 24, 2008 Author Share Posted November 24, 2008 Hi Mchl, Thanks, so in my example the changes I would make would be : [code=php:0] class db extends SystemBase { function __construct() { $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT); return $mysqli; } } // AND function db_connect() { require_once 'mysqli.php'; new db; return $db; } Am I doing the right thing with the mysqli extension? Would I be able to call the mysqli methods just like PEAR as long as I use the same nameing conventions as PEAR and build the same functionality in it? Is this a common thing people do? Also is it better to use the same filenames as the classes and use the __autoload() function? Quote Link to comment Share on other sites More sharing options...
trq Posted November 24, 2008 Share Posted November 24, 2008 Firstly, a constructor does not return a value, even if you try and force it too. Your database class should really keep the connection object hidden away within itself, I'm not sure where your trying to go with an external db_connect() function? Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted November 25, 2008 Author Share Posted November 25, 2008 Thanks Thorpe for pointing me in the right direction, OK, So I'd get rid of the constructor altogether and name that function something like connect(). Then when I want to initialise a connection I'd do something like : <?php new db; $db->connect(); ?> Yes I was thinking that function was a bit unnecessary. I was just following the framework tutorial. I've not had time to test these out, but I will spend some time on it tonight when I get home. Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted November 25, 2008 Share Posted November 25, 2008 So I'd get rid of the constructor altogether and name that function something like connect(). No, you would use your __costructor to establish a connection, but store it internally within the object. eg; class db extends SystemBase { private $mysqli; public function __construct() { $this->mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT); } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.