PHP tutorial: how to use namespaces

Hi guys! Today I will speak about the namespaces in PHP. In this tutorial I will show you a sample example to use the namespaces in PHP.

To use the namespaces, I will create a file index.php, a folder classes with a file first.php.

In my classes/first.php I will use this code:

namespace classes; // is name of the folder

class first{
public function show()
{
return 'Welcome ';
}
}

Next, to call this namespace in my index.php file, I will write this code:

use classes\first as first;
$a = new first();
echo $a->show();

// __autoload class will replace classes\first to classes/first.php

function __autoload($class)
{
$class = str_replace("\\","/",$class).".php";
require_once $class;
}

Video tutorial:









Leave a Comment