Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Circular reference of classes

$
0
0
I have a core class that does the majority of the heavy lifting for my framework.  
I also use an external class "access" to check for user access to resources.
While trying to streamline things a bit I noticed that when I create an "access"
object outside of my "core" class that I have to declare my "access" object as global
anytime I want to use it inside the "core" class.
 
Example:
class core()
{
public somefunction()
{
global $Access;
//... Etc ...
}
}
 
$Access = new access();
 
$Core = new core();
 
$Core->somefunction();
 
I want to do:
 
class core()
{
public somefunction()
{
//  $this->Access now available here w/o declare global
  //... Etc ...
}
public initAccess()
{
  $this->Access = new access();
//... Etc ...
}
}
 
 
The Problem is that "access" class uses the DB connection and query function inside "core".
I am worried about circular references.  Any gurus have advice on such an issue or am I worried about nothing?
 
Here is a snippet of the actual code pared down to minimum:
<?php
class myCore
{
	private $_Ini=array(); 	// Main Ini Info Array
	public $_DB='';			// DB Connection
	private $_ACCESS=false;	// Hold Access Class Instance
	
	public function __construct() {
		$this->_Ini 			= parse_ini_file(ABSPATH.DS."config.php", true);
		$this->connect();
		$this->initAccess();
	}// End Constuct

	public function connect()
	{
		if(!defined(HOST))
		{
			$dbauth = $this->_Ini['database'];
			define ('HOST',$dbauth['HOST']);
			define ('DB_USER',$dbauth['DB_USER']);
			define ('DBPASS',$dbauth['DBPASS']);
			define ('DB',$dbauth['DB']);
		}
		$this->_DB = @new mysqli(HOST, DB_USER, DBPASS,DB);

		/* check connection */
		if ($this->_DB->connect_error)
			trigger_error('Database connection failed in '.__FILE__.' at '.__LINE__.':'.$this->_DB->connect_error, E_USER_ERROR);
	} //end connect
	
	// Central Query Function
	public function DBquery($query,$Location) 
	{
		$result = $this->_DB->query($query) or
		trigger_error('<strong>Query Error:</strong> '.$this->_DB->error.
			'<br/><strong>Query Location:</strong> '.$Location.
			'<br/><strong>Query:</strong> '.$query, E_USER_ERROR);
		return $result;
	}// End DBquery()
	
	// Initiate Access class
	public function initAccess() 
	{
		$this->_ACCESS = new Access($_SESSION['UsrNum'],$this);
	}// End initAccess()
	
	/*
	 * Bunch of other Functions...
	 */
}



class Access
{
	/**
	* Checks user access for resources
	*/ 
    private $user;
    private $_CORE;

	public function __construct($user,&$CORE_Class)
	{
		$this->_CORE = $CORE_Class;
		$this->user = $user;
	} // End __construct
	
	public function getRole()
	{
		$result = $this->_CORE->DBquery('SELECT `group` FROM `user_role` WHERE `user`='.$this->user,__FILE__.' at '.__LINE__);
		while($row=$result->fetch_array(MYSQLI_ASSOC)) $role[]= $row['group'];
		return $role;
	} // End getRole
	
	/*
	 * Bunch of other Functions...
	 */
}

$MYcore = new myCore();
$MYcore->_ACCESS->getRole();
?>

 


Viewing all articles
Browse latest Browse all 13200

Trending Articles