开发者

How to achieve "application scope variables" in php?

开发者 https://www.devze.com 2023-02-01 09:03 出处:网络
I am working on a new PHP project now, this time I want to get the basics right at the beginning. Previously I\'ve found requiring/including files in php a bit of pain, please consider the following s

I am working on a new PHP project now, this time I want to get the basics right at the beginning. Previously I've found requiring/including files in php a bit of pain, please consider the following structure:

/application_root/index.php
                 /js/...
                 /css/...
                 /php/...
                 /conf/...
                 ...

In the index.php I can certainly use something like:

<link rel="stylesheet" href="css/sample.css" ... />
<script type="text/javascript" src="js/sample.js"></script>

To refer to the included css and js, or even php snippets. However, this would only work in the index.php which resides under the root of my application folder. I reckon this is no good.

I came across Java application configuration file "web.xml" where you can define application scope variables that you can simply refer to. .NET with C# has a similar thing. How to achieve this in simple php code so that from any php file in my app, I can type:

<?php echo "<link href='".$application_root_url."/php/sample.css' ..."; ?>

And it will evaluate to the right location?

I am thinking to use:

  • Global variables <== bad practice as violation to OOP? I stop doing this since c programming;
  • set_include_path <== so php will look f开发者_开发百科or it, requires unique name and proper naming convention?
  • load variables from ini files? <== how to make this happen?
  • any new thoughts?


You don't want to use global variables because they break encapsulation.

set_include_path will do no good especially if you are using those variables in HTML, because the include_path is relative to the application's filesystem path and not to its base url.

Determining the application base paths is generally not done from a configuration file, as it easy to detect when your application has a gateway script. Since those are constant values, it makes sense to define a constant:

define('APP_ROOT', dirname(__FILE__));
define('APP_URL', dirname($_SERVER['PHP_SELF']));

If you want to parse INI files however, you could use parse_ini_file, or parse_ini_string (>=5.3.0):

$config = parse_ini_file(APP_ROOT.'/'.CONFIG_DIR.'/database.ini');
echo $config['host'];


The path here is a URI--not a filesystem location. You may be trying to go about this the wrong way anyway.


You have to use relative path. So just "/css/sample.css" instad of "css/sample.css".

That would than always load from yourdomain.com/css/sample.css even if your .php is in yourdomain.com/somefolder/file.php


The PHP script only produces an output that the browser interprets. And the scope is the URL in the browser not on the filesystem.

So the value of the $application_root_url variable is for the browser not for the PHP script!

If you want to use INI files, you can use the parse_ini_file() function of PHP.


  1. no reason for using global variables except for lazy coding
  2. is not efficient, and PHP will get harder to figure which file to be included if two same filename on different path
  3. parse_ini_file is what you looking for

However, I prefer using constant, I did not ask to define constant everywhere, just put all your essential path into a config file, and require that at the beginning on your application.

Some might say constant is slow, comparing using class constant which might require you to include multiple files, which does better ? And the best things is once constant defined, no-one or code can override it.

example to illustrate

define('css_root', '/home/user/apache/css');  <-- server absolute path
define('css_web_root', '/css');               <-- web root, for HTML
define('css_cache_root', '/cache/css');       <-- cache directory


You might want to try something like the MVC pattern. As an example the ZendFrameworkds MVC passes a variable $this->base_url to the views. The views are where you HTML resides so you will be able to do the following in the view:

<link rel="stylesheet" href="<?php echo $this->base_url; ?>/css/sample.css" ... />

Your problem is exactly the reason that led me to the MVC pattern and it's many advantages.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号