Subnamespaces with PHP (Namespaces Part II)

Learn how to use subnamespaces with php the fast way :)

test.php (This is the example from previews blog entry)

<?php
namespace Test;

const TEST_IS_HERE = 1;

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

test_me.php

<?php
namspace Test\Me;
class TestSubClass {
	public function __construct() {}
	public function sayHello() {
		print „hello from TestSubClass“;
	}
}

# Here's a function in our subnamespace.
function sayJuhu() {
	print „juhu“;
}
?>

Using the namespaces

<?php
require_once 'test.php';
require_once 'test_me.php';

# Use subnamespace's class TestSubClass:
$testMe = new Test\Me\TestSubClass();
$testMe->sayHello();

# Using the subnamespace's function:
Test\Me\sayJuhu();

?>
  • 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>