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

Posted by Toby A Inkster on 05/18/07 08:02

Justin Voelker wrote:

> <?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;
> ...
> ?>

<?php
include('/additionalfiles/config.php');
class SlimStatConfig {
/** Database connection */
var $server = $GLOBALS['dbhost'];
var $username = $GLOBALS['dbuser'];
var $password = $GLOBALS['dbpasswd'];
var $database = $GLOBALS['dbname'];
....
?>

Though I'd agree with others that globals are evil. Better to use:

<?php
include('/additionalfiles/config.php');
class SlimStatConfig {
/** Database connection */
var $db;

public function __construct ($db)
{
$this->db = $db;
}
....
?>

And simply pass the object a fully-formed database connection when you
instantiate it.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

 

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

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