Namespace

Normally PHP does not allowed to use same multiple class name or method. So what you do on that situation. You can use Namespace. Namespace just like a virtual directory. You can use same multiple class name or method without a name collision.


/* first.php */
<?php

class A{

	function __construct(){

		echo "I am at global space </br>";
	}
}

/* second.php */
<?php

namespace second;

class A{

	function __construct(){

		echo "I am at second space <br/>";
	}
}


/* index.php */
<?php

include "first.php";
include "second.php";

use second\A;

//$obj = new second\A;	// qualified class name

$obj = new A;		// unqualified class name

$obj = new \A;		// fully qualified class name




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s