Today we will introduce some tips and tips for improving and optimizing PHP code. Note that these PHP skills are applicable to beginners, not those who are already using the MVC framework.
Today we will introduce some tips and tips for improving and optimizing PHP code. Note that these PHP skills are applicable to beginners, not those who are already using the MVC framework.
1. do not use relative paths. define a root path
Such code lines are common:
require_once('../../lib/some_class.php');
This method has many disadvantages:
It first searches for the specified directory in the php including path and then displays the current directory. Therefore, many directories are checked.
When a script is included in different directories of another script, its basic directory becomes the directory containing the script.
Another problem is that when a script runs from cron, it may not use its parent directory as the working directory.
Therefore, using absolute paths becomes a good method:
define('ROOT' , '/var/www/project/');require_once(ROOT . '../../lib/some_class.php');//rest of the code
This is an absolute path and will remain unchanged. However, we can further improve it. The directory/var/www/project can be changed. Do we need to change it every time?
No. use a magic constant such as _ FILE _ to make it portable. 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 . '../../lib/some_class.php');//rest of the code
So now, even if you move a project to a different Directory, such as moving it to an online server, the code can run without any changes.
2. do not use require, including require_once or include_once
Your script may contain various files, such as class libraries, utility files, and auxiliary functions, like these:
require_once('lib/Database.php');require_once('lib/Mail.php');require_once('helpers/utitlity_functions.php');
This is rather rough. Code needs to be more flexible. Writing a helper function makes it easier to include things. For 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');
Do you see the difference? Obviously. No more explanation is required.
You can further improve:
function load_class($class_name){ //path to the class file $path = ROOT . '/lib/' . $class_name . '.php'); if(file_exists($path)) { require_once( $path ); }}
This can accomplish many things:
Search multiple directories for the same class file.
Easily change the directory containing class files without damaging code anywhere.
Use similar functions to load files that contain helper functions and HTML content.
3. maintain the debugging environment in the application
During the development process, we echo database queries, dump the variables that create the problem, and once the problem is solved, we comment them or delete them. However, keeping everything in place can provide long-term help.
On the Development computer, 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. Send Status messages through sessions
Status messages are those generated after the task is executed.
...
Such code is very common. Using Variables to display status information has certain limitations. Because they cannot be sent through redirection (unless you spread them as GET variables to the next script, but this is silly ). In addition, there may be multiple messages in large scripts.
The best way is to use sessions for propagation (even on the same page ). To do this, you must have a session_start on each page.
function set_flash($msg){ $_SESSION['message'] = $msg;}function get_flash(){ $msg = $_SESSION['message']; unset($_SESSION['message']); return $msg;}
In your script:
Status is :
...
5. flexible functions
function add_to_cart($item_id , $qty){ $_SESSION['cart'][$item_id] = $qty;}add_to_cart( 'IPHONE3' , 2 );
Use the above function when adding a single entry. So when multiple entries are added, do I have to create another function? NO. You only need to make the function flexible so that it can accept different parameters. See:
function add_to_cart($item_id , $qty){ if(!is_array($item_id)) { $_SESSION['cart'][$item_id] = $qty; } else { foreach($item_id as $i_id => $qty) { $_SESSION['cart'][$i_id] = $qty; } }}add_to_cart( 'IPHONE3' , 2 );add_to_cart( array('IPHONE3' => 2 , 'IPAD' => 5) );
Now, the same function can accept different types of output. The above code can be applied to many places to make your code more flexible.