namespaces in PHP and autoloader


About namespaces:

About autoloading:

About SPL:

When the autoloader is located in the folder holding your classes, then use the relative namespace for the class. For instance, if all your classes are in a Vendor folder and you want to load the User class with the path Vendor/Model/user.php, then the classname to load is ‘Model\User’. In the example below the myautoloader.php file is held in the Vendor directory.

namespace Vendor;

class MyAutoloader
public static function autoload(){



$conn= new Connect\Connect;

Now we would need to recursively fetch the classes in the directory to load them automatically.

File System Iterator


$pathTop = __DIR__; //pathTop because we will recursively loop through directory starting from the current file directory at the top most category. Check get_url snippet to change that or use substr()

$directory = new RecursiveDirectoryIterator($pathTop, RecursiveDirectoryIterator::SKIP_DOTS); // this return an iterator sarting in the current file directory
echo $directory;

$fileIterator = new RecursiveIteratorIterator($directory,RecursiveIteratorIterator::LEAVES_ONLY);

foreach($fileIterator as $file){
	echo $file.'<br>';
	echo $path_array[count($path_array)-2];
	echo '<br>';
	echo $path_array[count($path_array)-1];

Incorrect integer value: ” for column …

Error message: Incorrect integer value: ” for column

Error description: the error occurs when inserting an empty value in a column.

Solution: This behavior is normal and shows that your sql-mod is set to strict. To change that access your server and look for the my.ini file. Locate the sql-mode line and set it as sql-mod=””

Get the time it takes to run a given function in PHP

To get the time in microsecond you will need to use microtime() instead of time().

You will need microtime() to return a float so set $getAsFloat to true like this:


Most of the function are really fast so to get something measurable you might need to run them several thousands time within a loop:

for( $i=0;$i<100000;$i++){
  //the function you want to test

You might want to use this method to compare two functions and verify which one is the fastest. You can then compare the time it took to run each of your function and pick the smallest one.

Optimizing PHP code using ticks

You can compare two ways of coding a script for the same end result by counting the number of ticks it takes to execute each version.

For this you can use register_tick_function() that allows you to execute a certain function at each tick.

For this to work you will need to use the declare function with the tick directive.

// A function called on each tick event
function tick_handler()
    global $c;

echo $c.'\n';

Keep in mind that some code might be more efficient than others for the same task due to their underlying C implementation. Or is it stricly equivalent (less tick== always faster)?

Persistent VS non-persistent MySQLi connection

“The idea behind persistent connections is that a connection between a client process and a database can be reused by a client process, rather than being created and destroyed multiple times. This reduces the overhead of creating fresh connections every time one is required, as unused connections are cached and ready to be reused.”

To open a persistent connection with mysqli you must prepend p: to the hostname when connecting.

Testing performance gain by using persistent connection on a joomla site was not conclusive:,script%20end%20and%20is%20reused.

What does the double colon (::) in PHP means?

The double colon is called the Scope Resolution Operator and is used to ” allows access  to staticconstant, and overridden properties or methods of a class.”

//Exemple 1
class MyClass {
    const CONST_VALUE = 'A constant value';

$classname = 'MyClass';
echo $classname::CONST_VALUE; // As of PHP 5.3.0

echo MyClass::CONST_VALUE;
//Exemple 2
class OtherClass extends MyClass
    public static $my_static = 'static var';

    public static function doubleColon() {
        echo parent::CONST_VALUE . "\n";
        echo self::$my_static . "\n";

$classname = 'OtherClass';
$classname::doubleColon(); // As of PHP 5.3.0