The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Object seems to behave like an array?
Discuss Object seems to behave like an array? in the PHP Development forum on Dev Shed. Object seems to behave like an array? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 29th, 2012, 04:17 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
|
Object seems to behave like an array?
Hi guys,
since i am playing around with oop i noticed something useful (i think). When I did a var_dump on a config class like below I got pretty much the same result as with an associative array. May I assume this is could be useful for config settings or is it better to maybe use a define() for these settings. I kind of like this object approach, since i can use the autoloader.
PHP Code:
class Config {
public $offline = '0';
public $display_offline_message = '1';
public $offline_image = '';
public $editor = 'tinymce';
public $captcha = '0';
// ..... etc
}
P.s. this is part of a joomla config file for those interested.
-edit: yes I really think this can be very usefull, just discovered the get_class_vars() function which could be very useful for loading configuration variables I think. Any thoughts?
Last edited by aeternus : December 29th, 2012 at 04:56 PM.
|

December 29th, 2012, 05:03 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Hi,
sure, you can use classes to store configuration values. However, you should use constants if the values are not supposed to change during runtime.
If the configuration is supposed to be "human readable", you might also use a specialized language like YAML or JSON (or XML) instead of PHP scripts. This lets you get rid of all the code overhead.
|

December 29th, 2012, 05:30 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
Quote: | Originally Posted by Jacques1 Hi,
sure, you can use classes to store configuration values. However, you should use constants if the values are not supposed to change during runtime.
If the configuration is supposed to be "human readable", you might also use a specialized language like YAML or JSON (or XML) instead of PHP scripts. This lets you get rid of all the code overhead. |
Thanks as always Jacques!
I haven't started that subject yet (as far as class constants), but It makes sense. This kind of data isn't meant to be changed indeed  You mean the class constants? or global defined constants? (someone said the later were slow)
Although I have no knowledge of code overhead  reading from an xml file into php sounds more costly (without anything to back that feeling up  ).
Thanks again!
Last edited by aeternus : December 29th, 2012 at 05:36 PM.
|

December 29th, 2012, 06:02 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
I mean class constants. They're simply defined in the class body with the "const" keyword.
Quote: | Originally Posted by aeternus Although I have no knowledge of code overhead  reading from an xml file into php sounds more costly |
Well, does it matter? If you actually do experience performance problems, they're unlikely to be caused by the application itself, let alone the parsing of some lines of YAML.
So don't avoid this just because you think it's expensive.
With "code overhead" I mean everything that isn't relevant for the configuration data itself and merely exists for the PHP interpreter (keywords, function calls etc.)
Last edited by Jacques1 : December 29th, 2012 at 06:06 PM.
|

December 29th, 2012, 06:05 PM
|
 |
For POny!
|
|
Join Date: Apr 2012
Location: Amsterdam
|
|
|
okay, cool. Thanks for the tips! I'l make sure i'll use them
-edit: that YAML looks cool! (too bad i can't run it very easily on windows) i'll might try it with a virtualmachine
Last edited by aeternus : December 29th, 2012 at 06:17 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|