Jump to content

Include Files in functions inside Class


Go to solution Solved by Barand,

Recommended Posts

Hi All,

 

I have created a PHP Class file which is called from my php page. This class contains many functions to do the database operations. 

 

For the database operations, I have created a different class (db.inc.php) which is included in my calling page. 

 

however, for each of the functions that I call from the class to do a database operation, I need to include the db.inc.php class in order to execute the functions. Kindly help me in avoiding this.

 

Calling PHP Page:

include "inc/db.inc.php";
 
 
PHP Class Functions:
function __construct()
{
include "db.inc.php";
$db = new mysqli_ext($mysql_hostname, $mysql_username, $mysql_password, $mysql_database);
 
}
 
public function request_app($app_id)
{
include "db.inc.php";
$arr_details; 
  $db = new mysqli_ext($mysql_hostname, $mysql_username, $mysql_password, $mysql_database);
$arr_details = $db->select("select a.*, b.APPL_NAME from tbldetails a, tbltype b where a.APPL_ID = b.APPL_ID and a.ID = ?", 's', $app_id);
return $arr_app_details;
}
 
 
If I remove the include from the above function, it just does not work. 
 
Thanks.
 
Link to comment
https://forums.phpfreaks.com/topic/279479-include-files-in-functions-inside-class/
Share on other sites

First, connecting to a database by using an include files is just silly when one can do something like this:

<?php # PDO database: only one connection is allowed. 

class Database {

  private $_connection;
  // Store the single instance.
  private static $_instance;
    
   // Get an instance of the Database.
   // @return Database: 
  public static function getInstance() {
    if (!self::$_instance) {
      self::$_instance = new self();
    }
    return self::$_instance;
  }
  
 // Constructor - Build the PDO Connection:
  public function __construct() {
   $db_options = array(
	   PDO::ATTR_EMULATE_PREPARES => false                     // important! use actual prepared statements (default: emulate prepared statements)
	   , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION           // throw exceptions on errors (default: stay silent)
	   , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC      // fetch associative arrays (default: mixed arrays)
   ); 		 
   $this->_connection = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'username', 'password', $db_options);
   
  }
    
   // Empty clone magic method to prevent duplication:
  private function __clone() {}
  
   // Get the PDO connection:    
  public function getConnection() {
    return $this->_connection;
  }
}	

You could do something like this for a mysqli type of connection:

 

Then you can autoload your class(es) in an include file (utilities.inc.php for example) that you would put at the top of you php file (index.php for example). 

// Autoload classes from "classes" directory:
function class_loader($class) {
	require('classes/' . $class . '.php');
}
spl_autoload_register('class_loader');

I would suggest reading an book on PHP Object-Oriented Programming (a recent version).

 

Oh, to connect you would do something like:

// Connect to Database:
$db = Database::getInstance(); // Actually it's an instance of a class, it's just worded badly. 
$pdo = $db->getConnection();
Edited by Strider64
  • Solution

You don't need, and shouldn't, reconnect before each query. It's usually the slowest part of the process. You only to connect once per page.

 

I'd connect to the database then pass $db to the class constructor. Inside the function you then reference $this->db

class foo {

private $db;

function __construct($db) {
   $this->db = $db;
}

function bar() {
    $result = $this->db->query("SELECT blah from table");
    ....
    return $blah;
}
}


$db = new mysqli(......);
$obj = new foo($db)

Hi Barand,

 

Thanks for your response. I tried your option & now getting the below error.

 

Catchable fatal error: Object of class mysqli_ext could not be converted to string 

 

Is there a way to declare the $db as a database object?

 

Thanks.

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.