• Welcome to Valhalla Legends Archive.
 

Access a class globally? (PHP)

Started by FrOzeN, November 26, 2006, 10:43 PM

Previous topic - Next topic

FrOzeN

How can I setup my MySQL_Wrapper class to have global access?

Eg:
index.php:
include 'mysql.class.php';
include 'login.class.php';

$clsMySQL = &New MySQL_Wrapper;
$clsLogin = &New LoginClass;

$clsLogin->something();


login.class.php:
class LoginClass {
    var $username;
    var $example;

    function something() {
        $sql_result = $clsMySQL->sql_query("SOMETHING ...");
        return $sql_result;
    }
}


mysql.class.php:
class MySQL_Wrapper {
    var $sql_connection;

    function sql_query($query) {
        $str_result = mysql_query($query, $this->sql_connection);
        return $str_result;
    }
}
~ FrOzeN

Ersan

#1
You can either use global $clsMySQL; (often considered bad programming, depends on how many of your classes will need to access the object), or pass it as a reference variable to the classes you're using (preferably through the constructor), like so:

index.php

$clsLogin = New LoginClass($clsMySQL);


login.class.php:

class LoginClass {
    var $mysql;
function LoginClass(&$mysql) {
    $this->mysql = &$mysql;
}
}


You really shouldn't be using &New to assign your mysql and login classes as reference variables either (I don't even think you're supposed to be able to).  Either way, you won't be able to call them from loaded classes that way.

FrOzeN

Thanks, that clears it up. I'll parse it through in the class initialization as you showed.
~ FrOzeN

Warrior

I wonder if you could use the Singleton Design Pattern in PHP hmm..
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

Ersan

#4
Not the same way you can in other languages, I really hate 'design patterns'.  Write what pertains to your program, globalizing anything in php wastes heaps of memory and is generally inefficient, if your class doesn't need access to the object, then don't waste memory giving it access.

As far as Singleton goes, the only way to accomplish it in PHP is to either write a function to create the object inside every class, or create an array of object variable references that you keep track of.  Either way you're globally referencing the object.

FrOzeN

#5
I got the class working but another problem has arose from this.

QuoteWarning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home2/frozen/public_html/myprose/mysql.class.php on line 42

Before the code in test.php was:
<?phpsession_start();include 'mysql.class.php';include 'login.class.php';$clsMySQL = New MySQL_Wrapper;$clsLogin = New LoginClass($clsMySQL);$clsMySQL->db_host = "localhost";$clsMySQL->db_username = "<snipped>";$clsMySQL->db_password = "<snipped>";$clsMySQL->db_database = "<snipped>";if ($clsMySQL->sql_connect())	echo "Connected to MySQL!<br />\n";if ($clsMySQL->select_db())	echo "Connected to MyProse Database!<br />\n";// ip check:$your_ip = $_SERVER['REMOTE_ADDR'];if ($clsMySQL->sql_query("SELECT * FROM ipaddresses WHERE ip='$your_ip'")) {	if (mysql_num_rows($clsMySQL->sql_result)) {		echo "Your IP Address exists in our database.";	} else {		echo "Your IP Address doesn't exist in our database.";	}}?>

That code still works fine.

Then I changed the bottom part to:
<?php/* ... snipped ... */// ip check:$clsLogin->check_ip();?>
And then added the function check_ip() to the LoginClass:
<?php/* ... snipped ... */function check_ip() {	$your_ip = $_SERVER['REMOTE_ADDR'];	if ($this->objMySQL->sql_query("SELECT * FROM ipaddresses WHERE ip='$your_ip'")) {		if (mysql_num_rows($this->objMySQL->sql_result)) {			echo "Your IP Address exists in our database.";		} else {			echo "Your IP Address doesn't exist in our database.";		}	}}/* ... snipped ... */?>



And that now gives me that error which I quoted at the start of my post. Also, for clarification my LoginClass is setup like so:
<?phpclass LoginClass{	var $objMySQL;	function LoginClass($mysql) {		$this->objMySQL = &$mysql;	}// etc..?>


Any idea why the mysql_ functions aren't working?

[EDIT] Just googled a bit and it says that it's caused due to the MySQL connection not establishing correctly. Why would that be if the MySQL establishes fine and runs queries when called from test.php?, yet not work when called from inside the LoginClass?
~ FrOzeN

rabbit

Connect to the sever and select the database before you reference the wrapper to anything else.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Ersan

#7
function LoginClass($mysql) {
that needs to be a reference var (like i told you the first time...):
function LoginClass(&$mysql) {

Should fix it.

FrOzeN

Quote from: Ersan on November 28, 2006, 03:01 PM
function LoginClass($mysql) {
that needs to be a reference var (like i told you the first time...):
function LoginClass(&$mysql) {

Should fix it.
Oops, didn't read your code closely enough. Works now. :)
~ FrOzeN