You are here: Re: Outside variables needed inside class « PHP Programming Language « IT news, forums, messages
Re: Outside variables needed inside class

Posted by Jerry Stuckle on 05/17/07 18:28

Justin Voelker wrote:
> 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!
>

No problem with never using classes before - there are a lot of PHP
programmers out there who haven't.

You can't define globals that way - in fact, generally it's bad to use
globals at all.

Rather, set up the values in your constructor - that's what it's there
for, i.e. (PHP5)

class SlimStatConfig {
/** Database connection */
var $server = "localhost";
var $username = "username";
var $password = "password";
var $database = "database";

function __construct ($s, $u, $p, $d) {
$this->server = $s;
$this->username = $u;
$this->password = $p;
$this->database = $d;
}
...
}

Then create it with something like:

$sl = new SlimStatConfig($dbhost, $dbuser, $dbpasswd, $dbname);

Or, if you really MUST use globals (even though you shouldn't):

function __construct () {
global $dbhost, $dbuser, $dbpasswd, $dbname;
$this->server = $dbhost;
$this->username = $dbuser;
$this->password = $dbpasswd;
$this->database = $dbname;
}

And call it with

$sl = new SlimStatConfig();


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация