Define namespaces with PHP (Namespaces Part I)

test.php

<?php
// Namespace needs to be the first statement in a file!
namespace Test;

const TEST_IS_HERE = 1;

class Trullala {
	public function __construct() {}
	public function sayHello() {
		print „hello“;
	}
}
?>

Using the namespace:

<?php
require_once 'test.php';

# To use constants, functions and classes of namespaces, you need to
# use \ after your namespace and add your constant, function or class name:
# namespace\[constant|function|class]
$trullala = new Test\Trullala();
$trullala->sayHello();

if (Test::TEST_IS_HERE == 1) {
	/* do something */
}
?>
  • Digg
  • del.icio.us
  • Facebook
  • Technorati
  • Twitthis
  • FriendFeed

POST A COMMENT

  • You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>