summaryrefslogtreecommitdiff
path: root/Zend/ZEND_CHANGES
diff options
context:
space:
mode:
authorSebastian Bergmann <sebastian@php.net>2003-02-24 10:35:39 +0000
committerSebastian Bergmann <sebastian@php.net>2003-02-24 10:35:39 +0000
commit57ae4a5ac42aeec38784246b390fd8ee0a1ae212 (patch)
treefef74d847f394b07c5c7de90beb54787ac3a8f9d /Zend/ZEND_CHANGES
parentcf72bfbae1e16fc9e2b727c154bae995df42dbfa (diff)
downloadphp-git-57ae4a5ac42aeec38784246b390fd8ee0a1ae212.tar.gz
Initial documentation of namespace {}.
Diffstat (limited to 'Zend/ZEND_CHANGES')
-rw-r--r--Zend/ZEND_CHANGES186
1 files changed, 21 insertions, 165 deletions
diff --git a/Zend/ZEND_CHANGES b/Zend/ZEND_CHANGES
index 50737429ba..588472c979 100644
--- a/Zend/ZEND_CHANGES
+++ b/Zend/ZEND_CHANGES
@@ -143,7 +143,7 @@ Changes in the Zend Engine 2.0
print $obj->address . "\n";
?>
- * Nested classes (namespaces).
+ * Namespaces.
The Zend Engine 1.0 provided only three scopes: the global
scope, the class scope and the function scope. All scopes but
@@ -153,177 +153,33 @@ Changes in the Zend Engine 2.0
Zend Engine 1.0's scoping methods were inherently limited for
solving symbol name collision problems.
- The Zend Engine 2.0 introduces the concept of nested classes
+ The Zend Engine 2.0 introduces the concept of namespaces
to solve the symbol collision problem by making it possible to
define multiple symbol tables able to contain all types of
- symbols. The Zend Engine is aware of a current class,
- defaulting to the global scope. Each class can contain it's
- own set of constants, functions and static variables. In order
- to access a class's local symbols you can use the self:: class
- accessor, for example, you can do self::$my_static_name = "Hello".
- You can also use the class's name such as
- MyClass::$my_static_name = "Hello". With both constants and
- functions, if you don't specify a class context the current class
- will be searched first and if the search fails then the global
- scope will be searched. If you want to force PHP to only check the
- global scope you can use the main:: accessor. For example,
- main::strlen() to make sure you're calling the strlen() in the main
- scope. You will only need to worry about this if you are defining
- methods which have the same name as global functions. For
- constants you can use the same notation such as self::MY_CONSTANT
- or main::MY_CONSTANT.
- Sometimes you will not want to access constants, functions or classes
- via the class accessor (i.e. MyClass::) because you use them very
- often and are an extremely slow typist. In this case, you can import
- functions, classes and constants from classes with the import keyword.
- It's quite self explanatory and there are a few examples below.
-
-
- * Classes may contain classes.
-
- Example:
-
- <?php
- class DB::MySQL {
- var $host = '';
-
- function db_connect($user) {
- print "Connecting to MySQL database '$this->host' as $user\n";
- }
- }
-
- class DB::Oracle {
- var $host = 'localhost';
-
- function db_connect($user) {
- print "Connecting to Oracle database '$this->host' as $user\n";
- }
- }
-
- $MySQL_obj = new DB::MySQL();
- $MySQL_obj->db_connect('Susan');
-
- $Oracle_obj = new DB::Oracle();
- $Oracle_obj->db_connect('Barbara');
- ?>
-
- * Classes may contain constants.
-
- Example:
-
- <?php
- class foo {
- const hey = 'hello';
- }
-
- print foo::hey;
- ?>
-
- * Current namespace's symbol tables are searched first for
- constants and functions.
+ symbols.
- Example:
-
- The following code prints "foobar", not "foo", because
- the class constant overrides the "global" constant of
- the same name.
-
- <?php
- define('foo', 'bar');
-
- class FooClass {
- const foo = 'foobar';
-
- function printFoo() {
- print foo;
- }
- }
- ?>
-
- * In the scope of a function, the current namespace is that
- of the containing class/namespace.
-
- Example:
-
- <?php
- class FooClass {
- function foo() {
- $this->bar();
- bar();
- }
-
- function bar() {
- print "foobar\n";
- }
- }
+ A namespace may contain classes, constants, functions and variables.
+ The member of a namespace is accessed by prefixing its name with the
+ name of the namespace followed by '::'.
- $obj = new FooClass;
- $obj->foo();
- $obj->foo();
- ?>
-
- This prints "foobar" two times, since a bar() method exists
- in the current namespace.
-
- * It is possible to "import" symbols from one namespace into
- another.
-
- Example:
-
- <?php
- class MyClass {
- class MyClass2 {
- function hello() {
- print "Hello, World in MyClass2\n";
- }
- }
-
- function hello() {
- print "Hello, World\n";
- }
- }
-
- import function hello, class MyClass2 from MyClass;
-
- MyClass2::hello();
- hello();
- ?>
-
- Example:
-
- <?php
- class MyOuterClass {
- class MyInnerClass {
- function func1() {
- print "func1()\n";
- }
-
- function func2() {
- print "func2()\n";
- }
- }
- }
-
- import class * from MyOuterClass;
- import function func2 from MyOuterClass::MyInnerClass;
-
- MyInnerClass::func1();
- func2();
- ?>
-
- Example:
+ Example:
- <?php
- class MyOuterClass {
- const Hello = "Hello, World\n";
- }
+ <?php
+ namespace Foo {
+ class Bar {}
+ const aConstant = 'someValue';
+ function aFunction() {}
+ var $aVariable;
+ }
- import const Hello from MyOuterClass;
- print Hello;
- ?>
+ $o = new Foo::Bar;
+ echo Foo::aConstant;
+ Foo::aFunction();
+ Foo::$aVariable = 'someValue';
+ ?>
- Old code that does not take advantage of namespaces will run
- without modifications.
+ To avoid ambiguities in the '::' resolution there may be no
+ global class and namespace with the same name.
* Unified Constructors.