Config files and classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Config files and classes

Post by therat »

I have 2 files as below. The first is a general config file that holds variables used throughout. The second is a class that will eventually create some reports. How do I call the config class inside the reprts class. I hope this makes sense, it's the first time I have tried to use classes.
Config.php

Code: Select all

<?php

	//create new config class
	$conf = new Config();

	// MySQL server name
	$conf->mysqlserver = 'localhost';
	// MySQL database user
	$conf->dbuser = 'username';
	// MySQL database name
	$conf->dbname = 'databasename';
	// MySQL database password
	$conf->dbpwd = 'password';
	// show flag of visitor
	// option are: 1 show flags, 0 do not show flags
	$conf->countryflags = 1;
	// flag extension
	$conf->imageextension = '.png';
	// flag directory
	$conf->countryflagspath = 'images/flags';

	class Config
	{
		// MySQL server name
		var $mysqlserver;
		// MySQL database user
		var $dbuser;
		// MySQL database name
		var $dbname;
		// MySQL database password
		var $userpwd;
		// show flag of visitor
		var $countryflags;
		// flag extension
		var $imageextension;
		// flag directory
		var $countryflagspath;
	}
	
?>
Reports.php

Code: Select all

<?

    class Reports
    {

    function Reports()
        {
            $this->startTimer();
        }
        

     function country()
        {
            echo "<img src='gb.png'>";
        }

		function startTimer() 
		{ //function to start the timer. 
			$mtime = microtime (); 
			$mtime = explode (' ', $mtime); 
			$mtime = $mtime[1] + $mtime[0]; 
			$this->starttime = $mtime; 
		} 

		function endTimer() 
		{ //function to end the timer. 
			$mtime = microtime (); 
			$mtime = explode (' ', $mtime); 
			$mtime = $mtime[1] + $mtime[0]; 
			$endtime = $mtime; 
			$totaltime = round (($endtime - $this->starttime), 5); 
			return $totaltime; 
		}
}
?>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Make a class constructor for the config class that does the following:

Code: Select all

// MySQL server name 
   $this->mysqlserver = 'localhost'; 
   // MySQL database user 
   $this->dbuser = 'username'; 
   // MySQL database name 
   $this->dbname = 'databasename'; 
   // MySQL database password 
   $this->dbpwd = 'password'; 
   // show flag of visitor 
   // option are: 1 show flags, 0 do not show flags 
   $this->countryflags = 1; 
   // flag extension 
   $this->imageextension = '.png'; 
   // flag directory 
   $this->countryflagspath = 'images/flags';
Then just call the class from inside the other class.
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

Thanks, i'll try that later
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

kettle_drum wrote:Make a class constructor for the config class that does the following:

Code: Select all

// MySQL server name 
   $this->mysqlserver = 'localhost'; 
   // MySQL database user 
   $this->dbuser = 'username'; 
   // MySQL database name 
   $this->dbname = 'databasename'; 
   // MySQL database password 
   $this->dbpwd = 'password'; 
   // show flag of visitor 
   // option are: 1 show flags, 0 do not show flags 
   $this->countryflags = 1; 
   // flag extension 
   $this->imageextension = '.png'; 
   // flag directory 
   $this->countryflagspath = 'images/flags';
Then just call the class from inside the other class.
I am doing something wrong because I cannot get this to work. Does the above get added to the actual class code already in config.php, or should it replace the top part of that file. Also, how do I call the class inside the reports.php file. I have always used includes up to now so this is a bit strange still.
Post Reply