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 ?

Community
  • 1
  • 1
user2005646
  • 148
  • 3
  • 16

2 Answers2

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');
Community
  • 1
  • 1
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
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.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103