Index: /trunk/Decompiler.class.php
===================================================================
--- /trunk/Decompiler.class.php	(revision 752)
+++ /trunk/Decompiler.class.php	(revision 753)
@@ -71,24 +71,26 @@
 }
 // }}}
+function unquoteName_($str, $asProperty, $indent = '') // {{{
+{
+	$str = str($str, $indent);
+	if (preg_match("!^'[\\w_][\\w\\d_\\\\]*'\$!", $str)) {
+		return str_replace('\\\\', '\\', substr($str, 1, -1));
+	}
+	else if ($asProperty) {
+		return "{" . $str . "}";
+	}
+	else {
+		return $str;
+	}
+}
+// }}}
 function unquoteProperty($str, $indent = '') // {{{
 {
-	$str = str($str, $indent);
-	if (preg_match("!^'[\\w_][\\w\\d_]*'\$!", $str)) {
-		return substr($str, 1, -1);
-	}
-	else {
-		return "{" . $str . "}";
-	}
+	return unquoteName_($str, true, $indent);
 }
 // }}}
 function unquoteName($str, $indent = '') // {{{
 {
-	$str = str($str, $indent);
-	if (preg_match("!^'[\\w_][\\w\\d_]*'\$!", $str)) {
-		return substr($str, 1, -1);
-	}
-	else {
-		return $str;
-	}
+	return unquoteName_($str, false, $indent);
 }
 // }}}
@@ -423,5 +425,6 @@
 class Decompiler
 {
-	var $rName = '!^[\\w_][\\w\\d_]*$!';
+	var $namespace;
+	var $namespaceDecided;
 
 	function Decompiler()
@@ -473,4 +476,29 @@
 				// }}}
 	}
+	function detectNamespace($name) // {{{
+	{
+		if ($this->namespaceDecided) {
+			return;
+		}
+
+		if (strpos($name, '\\') !== false) {
+			$this->namespace = strtok($name, '\\');
+			echo 'namespace ', $this->namespace, ";\n\n";
+		}
+
+		$this->namespaceDecided = true;
+	}
+	// }}}
+	function stripNamespace($name) // {{{
+	{
+		$len = strlen($this->namespace) + 1;
+		if (substr($name, 0, $len) == $this->namespace . '\\') {
+			return substr($name, $len);
+		}
+		else {
+			return $name;
+		}
+	}
+	// }}}
 	function outputPhp(&$opcodes, $opline, $last, $indent) // {{{
 	{
@@ -922,5 +950,5 @@
 						$class = $this->getOpVal($op2, $EX);
 						if (isset($op2['constant'])) {
-							$class = unquoteName($class);
+							$class = $this->stripNamespace(unquoteName($class));
 						}
 					}
@@ -930,10 +958,10 @@
 				case XC_FETCH_CONSTANT: // {{{
 					if ($op1['op_type'] == XC_IS_UNUSED) {
-						$resvar = $op2['constant'];
+						$resvar = $this->stripNamespace($op2['constant']);
 						break;
 					}
 
 					if ($op1['op_type'] == XC_IS_CONST) {
-						$resvar = $op1['constant'];
+						$resvar = $this->stripNamespace($op1['constant']);
 					}
 					else {
@@ -1191,5 +1219,5 @@
 						if ($opc == XC_INIT_STATIC_METHOD_CALL || /* PHP4 */ isset($op1['constant'])) {
 							$EX['object'] = null;
-							$EX['called_scope'] = unquoteName($obj, $EX);
+							$EX['called_scope'] = $this->stripNamespace(unquoteName($obj, $EX));
 						}
 						else {
@@ -1212,4 +1240,5 @@
 					break;
 					// }}}
+				case XC_INIT_NS_FCALL_BY_NAME:
 				case XC_INIT_FCALL_BY_NAME: // {{{
 					if (!ZEND_ENGINE_2 && ($ext & ZEND_CTOR_CALL)) {
@@ -1249,8 +1278,9 @@
 					$args = $this->popargs($EX, $ext);
 
-					$resvar =
-						(isset($object) ? $object . '->' : '' )
-						. (isset($EX['called_scope']) ? $EX['called_scope'] . '::' : '' )
-						. $fname . "($args)";
+					$prefix = (isset($object) ? $object . '->' : '' )
+						. (isset($EX['called_scope']) ? $EX['called_scope'] . '::' : '' );
+					$resvar = $prefix
+						. (!$prefix ? $this->stripNamespace($fname) : $fname)
+						. "($args)";
 					unset($args);
 
@@ -1305,7 +1335,7 @@
 
 						$fetchop = &$opcodes[$i + 1];
-						$impl = unquoteName($this->getOpVal($fetchop['op2'], $EX), $EX);
+						$interface = $this->stripNamespace(unquoteName($this->getOpVal($fetchop['op2'], $EX), $EX));
 						$addop = &$opcodes[$i + 2];
-						$class['interfaces'][$addop['extended_value']] = $impl;
+						$class['interfaces'][$addop['extended_value']] = $interface;
 						unset($fetchop, $addop);
 						$i += 2;
@@ -1577,5 +1607,7 @@
 					// }}}
 				case XC_DECLARE_CONST:
-					$resvar = 'const ' . unquoteName($this->getOpVal($op1, $EX), $EX) . ' = ' . str($this->getOpVal($op2, $EX));
+					$name = $this->stripNamespace(unquoteName($this->getOpVal($op1, $EX), $EX));
+					$value = str($this->getOpVal($op2, $EX));
+					$resvar = 'const ' . $name . ' = ' . $value;
 					break;
 				case XC_DECLARE_FUNCTION_OR_CLASS:
@@ -1709,5 +1741,5 @@
 				$ai = $op_array['arg_info'][$i];
 				if (!empty($ai['class_name'])) {
-					echo $ai['class_name'], ' ';
+					echo $this->stripNamespace($ai['class_name']), ' ';
 					if (!ZEND_ENGINE_2_2 && $ai['allow_null']) {
 						echo 'or NULL ';
@@ -1755,4 +1787,6 @@
 	function dfunction($func, $indent = '', $nobody = false) // {{{
 	{
+		$this->detectNamespace($func['op_array']['function_name']);
+
 		if ($nobody) {
 			$EX = array();
@@ -1770,5 +1804,5 @@
 		}
 
-		$functionName = $func['op_array']['function_name'];
+		$functionName = $this->stripNamespace($func['op_array']['function_name']);
 		if ($functionName == '{closure}') {
 			$functionName = '';
@@ -1799,4 +1833,6 @@
 	function dclass($class, $indent = '') // {{{
 	{
+		$this->detectNamespace($class['name']);
+
 		// {{{ class decl
 		if (!empty($class['doc_comment'])) {
@@ -1819,5 +1855,5 @@
 			}
 		}
-		echo $isinterface ? 'interface ' : 'class ', $class['name'];
+		echo $isinterface ? 'interface ' : 'class ', $this->stripNamespace($class['name']);
 		if ($class['parent']) {
 			echo ' extends ', $class['parent'];
Index: /trunk/decompilesample.php
===================================================================
--- /trunk/decompilesample.php	(revision 752)
+++ /trunk/decompilesample.php	(revision 753)
@@ -1,3 +1,7 @@
 <?php
+
+//* >= PHP 5.3
+namespace ns;
+// */
 
 abstract class ClassName
