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

Posted by jasondavis on Stack Overflow See other posts from Stack Overflow or by jasondavis
Published on 2010-01-22T20:42:17Z Indexed on 2010/04/06 5:43 UTC
Read the original article Hit count: 210

Filed under:
|
|
|
|

I am trying to figure out how I want to handle settings in my PHP app. I have pretty much decide that I would like to use a Confg class file so it will be autoloaded and flexible in the future. Below is some stuff I was playing with.

I know you cannot set a variable to popluate a Constant so I then try to use a public static property.

Why can I not set public static $ip = $_SERVER['REMOTE_ADDR']; ??

<?php
// config.class.php
class Config
{
    const URL = 'http://www.foo.com';
    const DB_User = 'dbname';

    public static $test = 'test string';
    public static $ip = $_SERVER['REMOTE_ADDR'];
}

///////////////////////////////////////////////////////
//index.php

// works
echo Config::URL;

// works
echo Config::$test;

// DOES NOT WORK
echo Config::$ip;

?>

© Stack Overflow or respective owner

Related posts about php

Related posts about class