Reply to Re: Outside variables needed inside class

Your name:

Reply:


Posted by ZeldorBlat on 05/17/07 18:22

On May 17, 1:38 pm, Justin Voelker <justin.voel...@gmail.com> 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!

Does the class have a constructor? If so, that's the place to pass in
the variables. It might look something like this:

public function __construct($dbhost, $dbuser, $dbpasswd, $dbname) {
$this->dbhost = $dbhost;
$this->dbuser = $dbuser;
$this->dbpasswd = $dbpasswd;
$this->dbname = $dbname;
}

Then, when you instantiate the class, you would do it like this:

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

[Back to original 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

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