Can I set a PHP class property from an existing variable? -


I'm trying to figure out how I want to handle settings in my PHP app. I have to make a lot of decision that I would like to use a config class file so that it will be automatically loaded and flexible in the future. Below are some things I was playing with.

I know that you can not set a variable to prevail a stable, so I try to use a public stable property

I Public stable $ ip = $ _SERVER ['REMOTE_ADDR'];

  & lt; Why can not I set up php //? Config.class.php class configuration {const url = 'http://www.foo.com'; Const DB_User = 'dbname'; Public stable $ trial = 'test string'; Public stable $ ip = $ _SERVER ['REMOTE_ADDR']; } ///////////////////////////////////////////////// ////// //index.php // Functions Echo Configuration :: URL; // Work Resonance Conf: $ test; // echo Config :: $ ip; ? & Gt;  

Although it does not answer your question (jspcal responds correctly Here's an alternative, a quick solution that you can use to fit your needs:

  & lt ;? Php // config.class.php class config {/ ** * instance container * @ wise config * / private static $ instance = null; / ** * Fixed Container * @ Sir Array * / Private $ constant = array ('url' => 'http://www.foo.com/', 'db_user' = & gt; 'dbname'); / ** * Option Container * @ Sir Array * / Private $ Option = Array (); / ** * initially not allowed outside * / private function __ constructs (// set option (can be loaded from external source) $ this- & gt; Option = array ('test' = & gt; 'test string', 'ip' = & gt; $ _SERVER ['REMOTE_ADDR']); } / ** * Cloning disabled / Private function __clone () {} / ** * Get a new instance of class / public function getInstance () {if (null === self: $ instance) {self :: $ instance = New self (); } Return Self: $ example; } / ** * Retrieve the value with a high priority constant * @parmal $ key * to get / public function __get ($ key) {if (isset ($ ---- $ [- $ key-] Constant [$ key])) this-> Constant [$ key]; } Otherwise (isset ($ this-> Option [$ key])) {$ this- & gt; Return option [$ key]; }} / ** * Set a new one or update the key / value pair * @ to set the absolute $ key * * to set the value of the * absolute $ value * / public function __set ($ key, $ value) {$ This- & gt; Option [$ of] = $ value; }} //////////////////////////////////////////// // ////// //index.php // works config $ config = getInstance (); Echo $ config- & gt; Url; Echo $ config- & gt; exam; Echo $ config- & gt; Ip;  Renewed:   Upgrade:  Not sure that you want a constant / option with that type of priority, this is just an example. 


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -