Jump to content

Recommended Posts

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?

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?

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?

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

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);

  }

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.