Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Pulling vars from a config into a class
Old 12-16-2008, 03:45 PM Pulling vars from a config into a class
Average Talker

Posts: 15
Trades: 0
Hello,

I've been having a bit of trouble getting this to work, maybe I'm going about it the wrong way. What I am trying to do is take a variable from lets say config.php and use it in my database.class.php class file so I can centralise my options.

Putting an include to the config in the top of the database class doesn't seem to pull the vars into the class. Putting it just inside the class doesn't work either. How can I pull the config variables into use for the functions in this class?

For example...
config.php
Code:
$dbhost = "localhost" ;
$dbuser = "root" ;
$dbpass = "pass" ;
database.class.config
Code:
require_once "config.php" ;

class db {

     function connect(){
          
          mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to database. ')

     }

}
Hopefully that helps make things a bit clearer... I need connect() to be using the vars from config!

Cheers guys...

Leonard

Last edited by Gentleman_; 12-16-2008 at 03:48 PM.. Reason: Forgot tab changed fields and tried to tab and linebreak and submitted early!
Gentleman_ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-16-2008, 04:22 PM Re: Pulling vars from a config into a class
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
The idea behind classes means that they have no concept of what is around them - they are contained objects and only care about what is contained within them.

In your situation the simplest thing to do would be to pass them in as parameters, either in the class instantiation or when calling the Connect method. Something like...

PHP Code:

//Config.php file include 

$dbhost "localhost" ;
$dbuser "root" ;
$dbpass "pass" ;

class 
db {
     var 
$dbhost;
     var 
$dbuser;
     var 
$dbpass;

     function 
db($dbh$dbu$dbp)
     {
           
$this->dbhost $dbh;
           
$this->dbuser $dbu;
           
$this->dbpass $dbp;
     }

     function 
connect(){
          
          
mysql_connect($this->dbhost$this->dbuser$this->dbpass) or die ('Error connecting to database. ')

     }

}

$db = new db$dbhost$dbuser$dbpass );
$db->Connect(); 
That is a rough outline, it uses PHP4 constructs but you might want to make use of the much better PHP5 features for OO if your host supports it.
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 12-16-2008, 06:30 PM Re: Pulling vars from a config into a class
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Stuart makes an excellent point! But for a direct answer, vars are not global (meaning that they are ignored with methods and objects) and you would have to invite them into the global scope in your function method:

PHP Code:
class db {
     function 
connect(){
          global 
$dbhost$dbuser$dbpass;
          
mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to database. ')
     }

Or, you can use constants which are static defined values which are auto global:

config:
PHP Code:
define('DATABASE_HOST' 'localhost');
define('DATABASE_USER' 'root');
define('DATABASE_PASS' 'pass'); 
class:
PHP Code:
class db {
     function 
connect(){
          
mysql_connect(DATABASE_HOSTDATABASE_USERDATABASE_PASS) or die ('Error connecting to database. ')
     }

__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 12-16-2008, 07:50 PM Re: Pulling vars from a config into a class
Average Talker

Posts: 15
Trades: 0
Cheers guys thats really cleared up a lot of things in my head and given my knowledge a boost in the how classes work stakes.

Stoots method seems to work well (after a bit of getting my head round things. Out of interest do you guys think this is a better way then defining the needed vars as globals?

Thanks a lot for the help guys.
Gentleman_ is offline
Reply With Quote
View Public Profile
 
Old 12-16-2008, 08:17 PM Re: Pulling vars from a config into a class
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Building your class scripts without global dependancy is a great way to make your scripts more portable between applications. In other words, you can quickly copy that class script file into another project and have it running quickly without defining new vars.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Pulling vars from a config into a class
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15444 seconds with 12 queries