|
Posted by Schraalhans Keukenmeester on 05/17/07 18:41
At Thu, 17 May 2007 10:38:23 -0700, Justin Voelker let his monkeys type:
> I have a configuration file that contains the host, username,
> password, and database name required for any database connections.
> The config file runs through a few if/then/else statements to
> determine if the website is local or remote and if it is local if it
> is the test site or "live" site then it sets the 4 variables. I am
> trying to implement a webstats package that has it's own config file.
> I would the new config file to be able to use the same variables as my
> config file. The problem I am encountering is that the new config
> file has all of its variables inside of a class and I can't seem to
> get to my variables from within it. Right now the file starts like
> this:
>
> <?php
> class SlimStatConfig {
> /** Database connection */
> var $server = "localhost";
> var $username = "username";
> var $password = "password";
> var $database = "database";
> ...
> ?>
>
> What I am envisioning is something to the effect of:
>
> <?php
> include('/additionalfiles/config.php');
> class SlimStatConfig {
> /** Database connection */
> global $dbhost;
> global $dbuser;
> global $dbpasswd;
> global $dbname;
> var $server = $dbhost;
> var $username = $dbuser;
> var $password = $dbpasswd;
> var $database = $dbname;
> ...
> ?>
>
> I have never worked with classes before. And please spare me the "how
> do you call yourself a php developer without knowing how to use
> classes." Right now I just would like to get this to work with a
> minimal amount of work. Thanks for your help!
Using global variables inside classes isn't recommended. If you can avoid
it, do so at (almost) any cost.
Either parse them to the class via a function (its constructor would be a
good place to do so):
class slimStatConfig {
var $server;
var $username;
var $password;
var $database;
var $conn;
function slimStatConfig($dbhost,$dbuser,$dbpasswd,$dbname) {
$this->server=$dbhost;
$this->username=$dbuser;
$this->password=$dbpasswd;
$this->database=$dbname;
if (!$this->$conn = @mysql_connect($this->server, $this->username,
$this->password) {
// send error message and finish gracefully
}
if (!@mysql_select_db($this->database, $this->conn)){
// send error message and finish gracefully
}
}
}
require ('/additionalfiles/config.php');
$ssc=new slimStatConfig($dbhost,$dbuser,$dbpasswd,$dbname);
....,or include the config file INSIDE the constructor and use the vars
directly:
[...]
function slimStatConfig() {
require ('/additionalfiles/config.php');
$this->server=$dbhost;
$this->username=$dbuser;
$this->password=$dbpasswd;
$this->database=$dbname;
if (!$this->$conn = @mysql_connect($this->server, $this->username,
$this->password) {
// send error message and finish gracefully
}
if (!@mysql_select_db($this->database, $this->conn)){
// send error message and finish gracefully
}
}
I am assuming you use PHP4. If not, omit the var keyword and define the
class variables public, private or protected (I think private's best here):
private $username; // etc
and your constructor:
public function slimStatConfig() //etc
or using the PHP5 __construct() function instead:
public function __construct() //etc
HTH
Sh.
Navigation:
[Reply to this message]
|