0

Possible Duplicate:
Can I include code into a PHP class?
Include file in array

I have this php code

   <?php
   class Config {

   public static $__routes = array(

   "Home"                    => "index.php",
   "Support"                 => "support.php",
   "Ads"                     => "ads.php",

   );

   }

   ?>

and i have ex.php file like this

"Upgrade"                 => "upgrade.php",
"Faq"                     => "faq.php",
"Tos"                     => "tos.php",

I want to include ex.php file in "public static $__routes array" which in "class"

Something to be like

   <?php
   class Config {

   public static $__routes = array(

   "Home"                    => "index.php",
   "Support"                 => "support.php",
   "Ads"                     => "ads.php",

   include 'ex.php';

   );

   }

   ?>

Please how i can do that ?

3
  • no, class makes everything different Commented Jan 24, 2013 at 0:45
  • 1
    Missed that, and the answer to your question is here Can I include code into a PHP class? Commented Jan 24, 2013 at 0:52
  • Why in the first place are you trying this? What is your goal? This looks more and more like "error by design" Commented Jan 24, 2013 at 0:53

2 Answers 2

1

You can't and I already explained why in your last question:

Each file must be valid PHP code on its own, because the include only happens at runtime (i.e. when this exact line of code is reached), not at parse time (i.e. when the file is loaded)

Now what's different here is that the alternative solutions do not work because you cannot initialize class properties with expressions (i.e. A + B), only with literals (scalar values and arrays of scalar values)

Since the property is static, you can't even use a constructor, you will have to assign it outside of the class:

class Config {

   public static $__routes = array(

       "Home"                    => "index.php",
       "Support"                 => "support.php",
       "Ads"                     => "ads.php",

   );

   // ...
}

Config::__routes += (include 'example.php');
Sign up to request clarification or add additional context in comments.

Comments

0

That is not going to work. You can't perform any sort of evaluations when declaring class properties. Why not just join the items?

If you simply MUST have some route configured in another file, then I would suggest making this property protected or private and making the caller utilize a static method to get this information. In this static method you could join the routes defined in the static property array with the routes defined in an array (not just an array segment like you show) in the include file and then return this merged array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.