convert array elements to objects
Posted: Thu Feb 28, 2008 8:24 am
~pickle | Please use
I usually just include the file on the top a page and be done with it. Since I need to build a page that features more than one language I tend to put language content in said language files (they're usually called en.php or de.php or it.php). Now I wanted to write an error class which is used to display error message. In this error class, I would like to use the language array from the language files. I wanted every element to be an object so that I can access it throughout the class and don't have to define it globaly. Therefore I have written a function which loops through all elements (I include them in the class constructor) and converts them to objects. The class looks like this:
Question is, does it make sense to create the objects like I did or is there an easier solution?
Thanks
~pickle | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi everyone
Not sure if this belongs in this category or the theory and design one. If the latter is the case, please move it for me.
To my question: I'd like to know if my, rather crude, solution for a class based problem is making any sense or not. Story goes like this: I've got a file which has nothing in it but an array of text elements I use throughout the project. It looks more or less like thisCode: Select all
$_TEXT['username'] = 'Name';
$_TEXT['password'] = 'Passwort';
//...
Code: Select all
class kerror {
function kerror() {
require('_languages/'.$_GET['lang'].'.php');
$this->populateArray($_TEXT);
}
function wrongEmail() {
// function to error
print $this->username." is wrong";
exit;
}
function populateArray($textElements) {
foreach ($textElements as $elementName => $elementValue) {
$this->$elementName = $elementValue;
}
}
}
Thanks
~pickle | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]