wizanda
PHP Streamlining Ideas Posted on: 2008/6/13 17:56
Helper
Joined:
2004/3/26 7:04
From Nottingham, UK
Posts: 2008
Here are a few ideas I’ve noticed that make a big difference to the speed of PHP code and files overall…..

Blank Space:
The biggest is blank space, indent, etc; this is all read by a computer as binary and is therefore processed, so remove all indents and blank space, its not necessary and ruins the purpose of PHP…..Don’t believe this? Try removing just the indents from any file and running it on an online server and see the difference in reading time….Especially in IE which seems to notice this even more then FF…..

Escaping Info Correctly:
There are two main ways to escape a line of code or instructions:
// Escapes the line it’s on…
/* */ Escapes an Area of code or info…
Many people when including their own head or GPL data, make a common mistake of exiting each line individually (by using // on each line), this means when its read, it opens and closes around each line and has something that must be processed for each line, before the code can be read….
It is far better to have /* at the start of the head, and */ to close it at the end, this means when read, it only has one escape to read for multiple lines, this also means if something accidentally gets put on the closing line */}, it doesn’t vanish, like it can when accidental added to // multiple lines}…


Including files:
Many people have no ideas about the differences between include or require and then making sure, we have include_once or require_once as standard for most operations in a large site.
Include_once:
This performs the operation of including the file, at the time the item is read in the file.
Require_once:
This must be included before the item can be performed.

So if you have functions that you know exists in other functions or classes and are needed for an operation to perform, then these need requiring; else the operation can not be finished until it is collected from the include at the time of the operation (which of course will cause a delay, whilst it waits for the info it needs), which obviously is to late, as that operation at the time should already have all constants, functions and classes before it can be operated.

The only time not to use _once is if an item will need multiple copies of that included contents; yet 95% of the time, it is far safer to use _once, encase of multiple including of the same file; that have somehow lead around in a full circle.

Includes are there when items happen, so a header/footer is loaded at the time of page loading, the contents behind the header should be ready when it is asked for.

If !not defined CONSTANTS
This is something you would think would work and is often seen to escape files unless another file/constant is loaded….
The problem with that though, is a CONSTANT is as the name suggests something constant, not changing that means…..
There is also numerous PHP tests/operations that were designed to do the same thing much better... method_exists, function_exists, file_exists with these your asking for the item the PHP is working on and so therefore taking less time to complete.

As mentioned previously we have include_once or require_once, this will then make sure that the file is there once over the whole system; else what happens if a file is asked for from two locations, within both locations the constant won’t be defined, as it is per file, yet the include_once or require_once will be per site.

The other major problem is these switches don’t work as well as they could as defined and define are not exactly the same thing….
If !defined(‘CONSTANT’); = if not defined constant?
Define (‘CONSTANT’, ‘’); = constant equals this, so the question is still not defined and hasn’t exactly been answered, it’s been told it equals something….
Defined (‘CONSTANT’); = that tells the original question; yes it is defined and then it doesn’t need further investigation, to then understand as what or if it even is defined in the first place.


Approximated Files
In most web development, if something comes from the same folder it doesn’t need redirecting to the root of the server first; you can simply place it as a file name and extension.
There are times in PHP this might not work, yet 95% of the time it works; even if the original reason of collecting the file is from different sources.
This saves vast amounts of resources in some cases, especially where people are using (dirname(_file_)).’/here.php’ which can be just put as ‘here.php’, there are even worse cases then that where we try and collect a global constant, and go to the root path and then to a file that is next door to it….


Converting File Address or Not
Using constants to place words is what it’s meant for, as its something constant; so in server location of a file or directory, its a good practise as a name can change only once and that is then final, then it can be given in an address, if these are also maintained as defined questions, it also keeps the constant alive in the system and adds security if they are not defined exit (); (the constants would need to have a security code though to work for none inclusion of files via the web)

Variables for server address though can be dangerous, as each time that file is requested; the definition of the variables must be tested, as they are variables.
Basically this is saying keep it simple…
If a file is one up folder ‘../’ same folder ‘name.ext’ you don’t need to be amazingly complicated in your approach to file locations, the reason for saying use this method is then if you wish to change a folder name; it isn’t inflicted by a million variables or even a constant change, which isn’t practical for complete beginners, unless configured and defined by a system for them to use.
Lastly and most importantly is, it takes far less time to go up one or two folders ‘../../’ then asking what each item for the file location is.


Example 
(7 questions)
PATH./modules/.$humpty[“dumpty”]./.$wall[“fall”].’index..$extensions[“type”]..php’;
Solution (2 questions)
’index..$extensions[“type”]..php’;


Sometimes these paths are needed, for instances Tag’s, where items can come from multiple places and so need the exact folder name, yet not always the root path as it has that from the first file, if inline with the files asked for…
The more you can shorten a path, the quicker reaching it will be….Think of it in terms of questions asking your self when you go on journey, you simplify it first….Not add extra stop off points, just to see if they are still there on your journey you make regularly…..Imagine if you first went outside your house, just to check if you have a house, to see if you have a bathroom each time you go…


Server Address with Spaces
Though space has been mentioned, it is really noticeable if you leave space in-between the addresses of files you’re trying to collect.
For instance:

PATH . ‘/modules’ . $some[“config”] . ‘index.php’

You’re basically asking it to convert all those spaces into code, there is no actual need for those spaces, other then to slow your PHP code down.

PATH.‘/modules’.$some[“config”].‘index.php’


To be continued……..

N B with U
Transfer Print PDF Bookmark Top
wizanda
Re: PHP Streamlining Ideas Posted on: 2008/10/29 21:15
Helper
Joined:
2004/3/26 7:04
From Nottingham, UK
Posts: 2008
Always return simple variables, don't return a massive task at the time of recalling within a function, as it adds overall weight to the system.

N B with U
Transfer Print PDF Bookmark Top
wizanda
Re: PHP Streamlining Ideas Posted on: 2008/11/9 14:02
Helper
Joined:
2004/3/26 7:04
From Nottingham, UK
Posts: 2008
Where practical write micro functions of large functions, breaking it up into key components, as to return a simple variable.
These can then be stored as an array (if wanted) which can be undone much quicker at the time of processing, to store large sums of data with reference points.
Quote:
The shorted distance a variable travels, the less work it has to do; the more functions to remember, the more confused it could get.

N B with U
Transfer Print PDF Bookmark Top
wizanda
Re: PHP Streamlining Ideas Posted on: 2009/4/23 10:46
Helper
Joined:
2004/3/26 7:04
From Nottingham, UK
Posts: 2008
Static containment:
Often we look at code from the perspective of what we need and not what would the computer need to recall…
So where practical you can vastly reduce loading times by not requiring everything each time, yet instead collecting it from stasis….

<?php
function getClass($ClassName){static $handlers;
if(isset(
$handlers[$ClassName])){$handle $handlers[$ClassName];
return 
$handle;}else{$FilePath 'folder/'.$ClassName.'.php';
require_once 
$FilePath;
$handlers[$ClassName] = new $ClassName();
$handle $handlers[$ClassName];
return 
$handle;}}
?>


Static class $handlers will hold vast amounts of required files information in stasis for use when needed….Thus Allowing functions, classes and even additional extended class file requirements, to be held within the field of stasis.
The required files can add additional weight if stored inside another class.


Ok due to what i would call an error in PHP, the code has now been edited since first posting it.
This is because if Static variables use !isset it will fill a reference for each time its called, inside the static variable.
If you return it when it isset, it can not auto fill the static variable each time its called, as it has already been returned.
This basically means many people in PHP code have done their questions backwards, as all questions that are called multiple times will save additional copies inside the static variables; (var_dump() these to check) thus making more memory usage overall.

N B with U
Transfer Print PDF Bookmark Top
Top Previous Topic Next Topic
Register To Post
 This site is Hosted by Servage.Net