namespaces in PHP and autoloader

References

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(){

    spl_autoload('Model\User');

}
}
spl_autoload_register('Classes\MyAutoloader::autoload');


$conn= new Connect\Connect;

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

File System Iterator

References:

$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
var_dump($directory);
echo $directory;

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

var_dump($fileIterator);
	
foreach($fileIterator as $file){
	echo $file.'<br>';
	$path_array=explode('\\',$file);
	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:

microtime(true);

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

$start=microtime(true);
for( $i=0;$i<100000;$i++){
  //the function you want to test
}
$end=microtime(true)
$total_runtime=$end-$start;

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

https://www.php.net/manual/en/function.register-tick-function.php

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.

https://www.php.net/manual/en/control-structures.declare.php

declare(ticks=1);
$c=0;
// A function called on each tick event
function tick_handler()
{
    global $c;
    $c+=1;
}

register_tick_function('tick_handler');
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.”

https://www.php.net/manual/en/mysqli.persistconns.php

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:

https://www.itoctopus.com/how-to-use-mysql-persistent-connections-on-joomla-sites#:~:text=A%20non%2Dpersistent%20MySQL%20is,script%20end%20and%20is%20reused.

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

https://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php

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

OtherClass::doubleColon();