Hello all,
May I suggest something in this area?
I hope it will resolve some problems related to namespaces and classes
ambiguity.
Let's first take a look at the classes.
In-general they represent two things: scope and data.
Scope is a thing that can't be changed without compiler efforts and contains
methods and constants. We access these things using :: separator.
2nd thing that I called data obviously contains class instance data that we
know under property name. We can change data during runtime and we do it
using executor. To access data we normally use -> separator. At this point
somebody may want to point me out to the methods (the scope!) accessed
with -> too. That's correct. But in this case we always mean $this or
$variable that points to the Data, where executor will find pointer to the
scope and will access it internally.
>From this perspective, namespace is nothing more but another layer of
scopes. Indeed, it's supposed to contain all things like classes, functions,
and even variables. If we make namespaces as power as this, they will be
useful in complicated situations when for example you need to work with a
3rd party framework that is full of classes, functions, and global variables
with _the_same_ names as the ones you use in your own project. So you need a
way to _isolate_ the framework and you need to do it _without_ any
modifications in its code or you will have maintain your own changes in the
foreign code each time the code is changed. With appropriately powerful
namespaces, its just matter of moving all the framework objects (I mean
classes, defines, functions, and global variables) into namespace. It comes
with its own namespaces? Not a problem (see below).
I suggest to remove class table and introduce scope table. This table will
contain classes and namespaces.
Technically speaking, with this approach namespace may contain other
namespaces, classes, variables, defines, and functions. To access scope's
item you still use :: separator. It's not that hard to access items at any
level without even knowing you access class's scope or namespace's. It does
not really matter! You only need to know names of the scopes and their
relationship.
There is always current scope, which is "global" or noname namespace by
default. It's created before any other items such as embedded functions or
classes are created. If you have thing1::thing2 in your code, executor
should first lookup current scope for thing1 and if not found, lookup in the
global namespace (unless current is global). You see, it does not really
matter if thing1 is a class or namespace. It's just a scope name to lookup
for and once namespaces and classes are all in one hash, it's quite trivial
to do.
Include, require and their "_once" brothers, should include the file and
make all its objects (like classes, functions, global variables) to be
created in the current namespace. So the isolation will be achieved easily,
you simply put include into a namespace and all the rest, including nested
includes, will put all thing into the namespace.
namespace isolated_space {
include "3rdpartyframework/library.inc";
}
There is one more difference between namespaces and classes. You can span a
namespace across multiple files and have multiple chunks of the same
namespace in one.
With "include" sample above, to access functions or classes, we'll use
either way:
1. reopen namespace and make call from it:
namespace isolated_space {
afuntioncall();
}
2. specify the namespace explicitly:
isolated_space::afunctioncall();
In both cases, current namespace will be swithched to isolated_space and all
things accessed by afunctioncall() will be looked up for in this namespace
first.
Think about namespaces like directories in the filesystem. You can specify
relative and absolute path. All other items like files, symlinks, devices,
etc have to exist in directories.
-jv
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php