To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Upgrading PHP

Upgrading PHP Issues concerning PHP version upgrades and future releases

Reply
 
Thread Tools Rate Thread Display Modes
Old 09-16-2004, 06:55 PM   #1
neil
Junior Member
 
Join Date: Sep 2002
Posts: 19
config class help

What is the best way to have a config consisting of a set of variables and error messages set in a class?

At the moment I have it at the top of each method that uses it. Some people may say that is bad practice, but I'm trying to see about another solution.

I was thinking about a class like:
PHP Code:
class config {
function
__construct(){
    const var =
"asdf";
    const
var2 = 2;
    }
}
Not even sure the construct is needed ..and usage i would do something like config::var, but my variable formats are in the array types at the moment ($err['err_type'] = "error message";).. so is there anyway to preserve that format in a class, not sure if a constant can be assign as an array hehe.

Any help appreciated, thanks.
neil is offline   Reply With Quote
Old 09-16-2004, 07:19 PM   #2
Shrike
Not Yet Involved
 
Shrike's Avatar
 
Join Date: Oct 2003
Location: The Eighth, Sursamen
Posts: 2,254
Here's the static approach which your suggesting.
PHP Code:
class Config
{
  static
private $errors = array(
    
'err1' => "Error 1",
    
'err2' => "Error 2",
  );
  static
public function getError($key)
  {
    return
self::$errors[$key];
  }
}
Config::getError("err1");
Another common way to handle error messages is to put them in a bunch of regular constants in a file, and include them at the top of each script. They will be visible in every context.
PHP Code:
define("err1", "Error 1");
define("err2", "Error 2");
__________________
The Hundredth Idiot
Shrike is offline   Reply With Quote
Old 09-16-2004, 07:29 PM   #3
neil
Junior Member
 
Join Date: Sep 2002
Posts: 19
Thanks Shrike that looks like the one I may go for. Gonna take a while to convert all my config into this format, but should be best for the long run?

So I assume there is no way have a call like Config::err['error_type'] (directly calling the variable array instead of the get function)?
neil is offline   Reply With Quote
Old 09-16-2004, 07:46 PM   #4
Shrike
Not Yet Involved
 
Shrike's Avatar
 
Join Date: Oct 2003
Location: The Eighth, Sursamen
Posts: 2,254
You could, by changing the access type of the array to public. But having a get method is better practice, member variables should really only be accessed within a class.

Out of interest, I would probably opt for either the 2nd method, or for putting the errors in a database, depending on how much content management is needed.
__________________
The Hundredth Idiot

Last edited by Shrike; 09-16-2004 at 07:53 PM.
Shrike is offline   Reply With Quote
Old 09-16-2004, 08:20 PM   #5
neil
Junior Member
 
Join Date: Sep 2002
Posts: 19
So it would look something like this?:
public $err['err_type'] = "error message";

Although I get the error:
Code:
Parse error: syntax error, unexpected '[', expecting ',' or ';'
The one you prefered (the defines), you can't really organise them with arraytypes, sure ya can't? e.g. define("err2['error_name']", "Error 2"); | Using: Config::err2['error_name']

Thanks for helping by the way
neil is offline   Reply With Quote
Old 09-16-2004, 10:13 PM   #6
neil
Junior Member
 
Join Date: Sep 2002
Posts: 19
Hehe, sorry Shrike got it now.

Going to use that first method you gave and will keep the getmethod as well (as you say, it is good practice too!).

There isn't any performance drawbacks for this config setup or anything?

Edit: but then the config::$error['err_name'] looks more like a variable than the other (better readablity)... hehe I'm stuck again in which to choose.

Last edited by neil; 09-16-2004 at 10:45 PM.
neil is offline   Reply With Quote
Old 09-17-2004, 02:44 AM   #7
neil
Junior Member
 
Join Date: Sep 2002
Posts: 19
Just got the error:
Code:
Parse error: syntax error, unexpected '.', expecting ')'
Cause it most likely because the array is in a class:
PHP Code:
class Config
{
  static
private $errors = array(
    
'site' => "http://" . $_SERVER['HTTP_HOST'],
    
'version' => 0.2
  
);
  static
public function getError($key)
  {
    return
self::$errors[$key];
  }
}
Config::getError('site');
Does anyone know how to fix this?
neil is offline   Reply With Quote
Old 09-17-2004, 04:53 AM   #8
Shrike
Not Yet Involved
 
Shrike's Avatar
 
Join Date: Oct 2003
Location: The Eighth, Sursamen
Posts: 2,254
You can't set member variables dynamically (i.e. by using $_SERVER in the definition.). This is the case in PHP 4 + 5. You would have to do it in a class method, or use a literal string.
__________________
The Hundredth Idiot
Shrike is offline   Reply With Quote
Old 09-17-2004, 11:37 AM   #9
neil
Junior Member
 
Join Date: Sep 2002
Posts: 19
This is gonna be a stupid question, but how do you set that to be a strong literal?

=> "http:// \$_SERVER['HTTP_HOST']",

Tried something like that, but no go.

Setting the arrays in a class method would go like (works but just wondering if you think it looks right):
PHP Code:
  static private function errors($key)
  {
     
$errors = array(
      
'site' => "http://" . $_SERVER['HTTP_HOST'],
      
'version' => 0.2,
     );
    return
$errors[$key];
  }
  static
public function getError($key)
  {
    return
self::errors($key);
  }
neil is offline   Reply With Quote
Old 09-17-2004, 11:42 AM   #10
Shrike
Not Yet Involved
 
Shrike's Avatar
 
Join Date: Oct 2003
Location: The Eighth, Sursamen
Posts: 2,254
You can't use a variable (i.e. in your case $_SERVER['HTTP_HOST']) inside of the array definition. Only string literals (i.e. 'www.mysite.com' or something)
__________________
The Hundredth Idiot
Shrike is offline   Reply With Quote
Old 09-17-2004, 11:49 AM   #11
neil
Junior Member
 
Join Date: Sep 2002
Posts: 19
Ahh, I getcha now.

Thanks for your advice, you've been really helpful :-)

By the way, does that method look alright to you? it works, but was just curious if ya think I did anything wrong with it.
neil is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 02:18 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.