PHP学习指南
http://www.binarytides.com/category/php-2/tutorial/
In this series we are going to look into some useful tips and techniques that can be used to improve and optimise your php code. Note that these php tips are meant for beginners and not those who are already using mvc frameworks etc.
The Techniques 1. Do not use relative paths , instead define a ROOT path
Its quite common to see such lines :
require_once('http://www.cnblogs.com/lib/some_class.php');
This approach has many drawbacks :
It first searches for directories specified in the include paths of php , then looks from the current directory.
So many directories are checked.
When a script is included by another script in a different directory , its base directory changes to that of the including script.
Another issue , is that when a script is being run from cron , it may not have its parent directory as the working directory.
So its a good idea to have absolute paths :
define('ROOT' , '/var/www/project/');require_once(ROOT . 'http://www.cnblogs.com/lib/some_class.php');//rest of the code
Now this is an absolute path and will always stay constant. But we can improve this further. The directory /var/www/project can change , so do we change it everytime ? No instead we make it portable using magic constants like __FILE__ . Take a closer look :
//suppose your script is /var/www/project/index.php//Then __FILE__ will always have that full path.define('ROOT' , pathinfo(__FILE__, PATHINFO_DIRNAME));require_once(ROOT . 'http://www.cnblogs.com/lib/some_class.php');//rest of the code
So now even if you shift your project to a different directory , like moving it to an online server , the same code will run without any changes.
2. Dont use require , include , require_once or include_once
Your script could be including various files on top , like class libraries , files for utility and helper functions etc like this :
require_once('lib/Database.php');require_once('lib/Mail.php');require_once('helpers/utitlity_functions.php');
This is rather primitive. The code needs to be more flexible. Write up helper functions to include things more easily. Lets take an example :
function load_class($class_name){ //path to the class file $path = ROOT . '/lib/' . $class_name . '.php'); require_once( $path ); }load_class('Database');load_class('Mail');
See any difference ? You must. It does not need any more explanation.
You can improve this further if you wish to like this :
function load_class($class_nam<a>@本文9来源gao($daima.com搞@代@#码8网^</a><strong>搞代gaodaima码</strong>e){ //path to the class file $path = ROOT . '/lib/' . $class_name . '.php'); if(file_exists($path)) { require_once( $path ); }}
There are a lot of things that can be done with this :
Search multiple directories for the same class file.
Change the directory containing class files easily , without breaking the code anywhere.
Use similar functions for loading files that contain helper functions , html content etc.
3. Maintain debugging environment in your application
During development we echo database queries , dump variables which are creating problems , and then once the problem is solved , we comment them or erase them. But its a good idea to let everything stay and help in the long run
On your development machine you can do this :
define('ENVIRONMENT' , 'development');if(! $db->query( $query ){ if(ENVIRONMENT == 'development') { echo "$query failed"; } else { echo "Database error. Please contact administrator"; } }
And on the server you can do this :
define('ENVIRONMENT' , 'production');if(! $db->query( $query ){ if(ENVIRONMENT == 'development') { echo "$query failed"; } else { echo "Database error. Please contact administrator"; } }
4. Propagate status messages via session