Jump to content

Include Files in functions inside Class


santosh22

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

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)

Archived

This topic is now archived and is closed to further replies.

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