Question : instantiating an object in php

Hi,
      just want to understand something ...
i have the following index.php file
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
<?php
require_once("../includes/database.php");

if(isset($database)) { echo "true"; } else { echo "false"; }
echo "<br />";

echo $database->mysql_prep("It's working?<br />");


?>

and when it is accessed  in a browser window produces the following output;
true
It\'s working?
what i think is happening is the following;
when the index.php file is called in the browser, the php server runs the index.php file and loads the database.php file into the server ram. (require_once("../includes/database.php");)
the database.php file is run by the php server and the index.php  file run is temporarily suspended...
the database.php file creates an instance of MySQLDatabase, (which is instantiated and put into the server ram) called $database before handing back control to the index.php file..
the index.php file then checks to see if $database exists and on true outputs what we see in the browser window....

is this actually what happens?
any help/ advice greatly appreciated...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
//database.php
//
<?php
require_once("config.php");

class MySQLDatabase {
	
	private $connection;
	
  function __construct() {
    $this->open_connection();
  }

	public function open_connection() {
		$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
		if (!$this->connection) {
			die("Database connection failed: " . mysql_error());
		} else {
			$db_select = mysql_select_db(DB_NAME, $this->connection);
			if (!$db_select) {
				die("Database selection failed: " . mysql_error());
			}
		}
	}

	public function close_connection() {
		if(isset($this->connection)) {
			mysql_close($this->connection);
			unset($this->connection);
		}
	}

	public function query($sql) {
		$result = mysql_query($sql, $this->connection);
		$this->confirm_query($result);
		return $result;
	}
	
	public function mysql_prep( $value ) {
		$magic_quotes_active = get_magic_quotes_gpc();
		$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
		if( $new_enough_php ) { // PHP v4.3.0 or higher
			// undo any magic quote effects so mysql_real_escape_string can do the work
			if( $magic_quotes_active ) { $value = stripslashes( $value ); }
			$value = mysql_real_escape_string( $value );
		} else { // before PHP v4.3.0
			// if magic quotes aren't already on then add slashes manually
			if( !$magic_quotes_active ) { $value = addslashes( $value ); }
			// if magic quotes are active, then the slashes already exist
		}
		return $value;
	}
	
	private function confirm_query($result) {
		if (!$result) {
			die("Database query failed: " . mysql_error());
		}
	}
	
}

$database = new MySQLDatabase();
$db =& $database;

?>

Answer : instantiating an object in php

yes
Random Solutions  
 
programming4us programming4us