This is an abbreviated version of the lang.Enum class:
namespace lang;
class Enum extends Object {
publicstaticfunction valueOf(XPClass $class, $name){ if(!$class->isEnum()){ thrownew IllegalArgumentException( 'Argument class must be lang.XPClass<? extends lang.Enum>' ); } try{ return array_values($class->_reflect->getStaticProperties()); }catch(ReflectionException $e){ thrownew IllegalArgumentException($e->getMessage()); } }
publicfinalfunction __clone(){ raise('lang.CloneNotSupportedException', 'Enums cannot be cloned'); } }
What can be seen:
The namespace declaration comes at the top of the file. It is indented
to the same level the class declaration starts at.
Classes from the lang package (Object, XPClass, IllegalArgumentException)
are not fully qualified
The ReflectionException class (builtin) is without namespace and
therefore explicitely referenced that way (though that would not be
necessary in this case!)
The raise() core functionality is also referenced as being in the
global namespace. This is absolutely necessary - the implicit import
of builtin classes and functions does not work here!
Syntax example #2
namespace iocollections;
class FileCollection extends langObject implements IOCollection {
publicfunction open(){ if(!($this->_hd= opendir($this->uri))){ thrownew ioIOException('Could not open '.$this->uri); } }
XP packages map to PHP namespaces by replacing the dots (.) with
double colons (Package[io.collections] = Namespace[io\collections])
XP class names map to PHP namespaced names by replacing the dot (.)
with double colons (XPClass[util.Date] = Class[util\Date]).
Classes from the same package are unqualified (IOCollection), while
classes from foreign packages (IOException, Date, Object) are written
using their fully qualified names.
The Object class is used in its fully qualified form. An auto-import
for all classes in the "lang" package as known from the Java programming
language is not available due to the generic way namespaces are
implemented in the PHP language!
uses() vs. use
We figured the following would be rather ugly:
uses('com.oneandone.qf.QfId');
use comoneandoneqfQfId;
$id= new QfId();
...because from looking at this, somehow the import seems to happen twice.
In reality though, uses() loads classes by their fully qualified
name by means which boil down to include.
We basically came up with the following alternatives:
Add special syntax to uses() such as com.oneandone.qf.QfId:QfId or
prefixing class names with "+" for importing
Rely on autoloading
Don't change anything and use the above
From these, the second sounds promising - given that autoload isn't as
slow as everybody's saying (this will need to be profiled) - it's
also the sexiest. Here's an idea:
QfId.class.php
namespace comoneandoneqf;
class QfId extends langObject { protected$value= 0;
- friebe, Sun Sep 2 20:34:58 2007
The migrated version currently does not contain import statements but
instead ::uses() and only fully qualified names for foreign packages.
The reason for this is that an __autoload() / import constellation
has some weird side effetcs that need to be investigated.