jcadima
4/9/2016 - 5:53 PM

PHP NameSpaces

PHP NameSpaces


<?php

// class Client, this is in Global Namespace
class Client {
	// code ...
}


// class Twitter , this is in Global Namespace
class Twitter {
	// code ...
}



ini_set('display_errors', 1) ;

include './src/HTTP/Client.php';
include './src/Twitter/Client.php';

var_dump(new Client) ;

var_dump(new Client) ;

// OUTPUT: will create a Fatal Error cannot redeclare Client 


// ###########The following will work because of Namespaces ####################

// class Client
namespace HTTP;
class Client {
	// code ...
}

// class Twitter
namespace Twitter
class Twitter {
	// code ...
}


ini_set('display_errors', 1) ;

include './src/HTTP/Client.php';
include './src/Twitter/Client.php';

// specify their namespaces
var_dump(new HTTP\Client) ;

var_dump(new Twitter\Client) ;