Whoops, there is a use statement. Sorry.
名前空間の定義
名前空間の宣言は、namespace キーワードを用いて行います。これを、ファイルの先頭に記述しなければなりません。 たとえば次のようになります。
例1 名前空間の定義
<?php
namespace MyProject::DB;
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
?>
名前空間の中にはクラスや定数、関数定義を含めることができます。 ただしそれら以外のコードを含めることはできません。
名前空間の定義は次のようなものです。
- 名前空間の内部では、すべてのクラスや関数、 定数名には自動的に名前空間名のプレフィックスが付加されます。 クラス名は常にフルネームとなります。つまり、 上の例のクラスをコールする際は MyProject::DB::Connection とします。
- 定数を定義すると、名前空間名と定数名を組み合わせた定数を作成します。 クラス定数と同様、名前空間定数にも静的な値しか保持できません。
-
修飾されていないクラス名 (:: を含まない名前) は、実行時に次の手順で解決されます。
- そのクラスを、現在の名前空間から (つまり現在の名前空間名を先頭につけて) 探します。 その際には autoload を試みません。
- グローバル名前空間から、autoload を試みずにそのクラスを探します。
- 現在の名前空間で autoload を試みます。
- 以上がすべて失敗した場合は、クラスの検索が失敗します。
-
修飾されていない関数名 (:: を含まない名前) は、実行時にまず現在の名前空間で探され、 次にグローバル空間で探します。
-
修飾されていない定数名は、 まず現在の名前空間で探され、次にグローバル空間で探します。
名前空間の定義
info at thewebsiteguy dot com dot au
02-Oct-2008 05:28
02-Oct-2008 05:28
PHP Namespaces will be excellent. I write some strict OO n-Tier apps (as much as possible with PHP) so this will be good for separating layers. The only thing I'll add is that since I have been a C# programmer, I would have personally preferred name spaces to be encapsulated in {} and use an import or using statement OR FQ namespace for the class in the case of an ambiguous class.
As an example I might create some classes I use to present the page (suffixed "Module"), and they relate to a CBO of the same name but with "CBO" as a suffix. Now I can have less classes to filter through in the IDE!
Now if only we can have partial classes :P
David Drakard
07-Sep-2008 08:56
07-Sep-2008 08:56
I agree with SR, the new namespaces feature has solved a number of problems for me which would have required horrible coding to solve otherwise.
An example use:
Say you are making a small script, and write a class to connect to a database, calling it 'connection'. If you find your script useful and gradually expand it into a large application, you may want to rename the class. Without namespaces, you have to change the name and every reference to it (say in inheriting objects), possibly creating a load of bugs. With namespaces you can drop the related classes into a namespace with one line of code, and less chance of errors.
This is by no means one of the biggest problems namespaces solve; I would suggest reading about their advantages before citicising them. They provide an elegant solutions to several problems involved in creating complex systems.
Baptiste
15-May-2008 03:47
15-May-2008 03:47
There is nothing wrong with PHP namespaces, except that those 2 instructions give a false impression of package management.
... while they just correspond to the "with()" instruction of Javascript.
By contrast, a package is a namespace for its members, but it offers more (like deployment facilities), and a compiler knows exactly what classes are in a package, and where to find them.
SR
13-May-2008 07:53
13-May-2008 07:53
RS: Please go read a book on object oriented programming so you understand why namespaces are important, instead of actively discouraging their use through bad examples that show your lack of basic understanding towards them.
First of all a well designed reusable module would use a better namespace than something as simple and useless as "Gizmo". They are plenty of ways of handling this, for example if you look at the Java* world you'll notice the norm here is the "reverse URL" way, ie. all Apache Foundation projects are contained in "org.apache.<project name>" namespaces (in PHP the :: is used as namespace seperator so the PHP equivalent would be "org::apache::<project name>") Also keep in mind that a project can have multiple namespaces inside it, which is not obligatory but provides an additionnal way of cleanly seperating code in distinct units.
Secondly, when using tokens from external namespaces, you are meant to export them through the "use" keyword at the top of your code. This allows you to maintain tight control over external interfaces. It also avoids repeating the same "base name" of your class over and over and over in your code, which makes it cleaner and easier to refactor.
Namespaces were the missing link preventing PHP 5 from becoming a real object oriented language. Even if YOU are not interested in using them in your projects, you may want to not be so quick at dissing them.
* Java namespaces go by the cheesy name of "packages" but they basically work as you'd expect namespaces to do in any other language
Valeriu Palo
04-May-2008 01:57
04-May-2008 01:57
I see nothing wrong with this implementation.
Having free code disallowed in namespaces is (I think) a good thing. If namespaces are used in a script, it is quite clear that the script is strongly object oriented, and thus the usage of variables and code that are "raw" in a namespace is nothing less than weird! That functionlity would much better fit inside a class (or a Singleton?). It is the same principle behind what makes global variables a bad choice.
As far as the naming conventions go, a namespace is only meant to help you "isolate" your constructs from what others might accidentally create and then clash with your scripts. Of course one could potentially use the same identifiers as another but the point is that such an act would be intentional and not accidental! When you create a library, you might want to put everything inside a namespace that is intuitively connected to you or your project and not something that someone else might be inclined to use.
Alas, this truly has NOTHING to do with the language!
Anonymous
02-Apr-2008 03:11
02-Apr-2008 03:11
@ RS: Also, you can specify how your __autoload() function looks for the files. That way another users namespace classes cannot overwrite yours unless they replace your file specifically.
someone3x7 at hotmail dot com
06-Mar-2008 03:57
06-Mar-2008 03:57
RS, I've mostly used namespaces for Read-ability. And to collect those few loose low level functions that, while necessary, have no business in an object with the classes thier almost related too. Its also good to keep the Global namespace scope clean of unnecesary symbols, which in larger projects affects performance slightly and makes debugging easier. I'm sure I could go on, yet the Read-ability should be enough for most.
FB
28-Feb-2008 08:05
28-Feb-2008 08:05
RS wrote on 27-Feb-2008 07:31:
"I don't see how this implementation of namespaces gives you anything you didn't already have.
Let's say someone is writing a set of classes to handle gizmos... most developers would write something like:
<?php
class GizmoBird() {
}
class GizmoPlane() {
}
?>
Now with namespaces, this would look like:
<?php
namespace Gizmo;
class Bird() {
}
class Plane() {
}
?>
But this doesn't really help you isolate your classes any better. someone else could still write another set of classes with the same namespace, and Gizmo::Bird would clash with another developer's Gizmo::Bird, in the exact same way that GizmoBird would clash with another developer's GizmoBird.
So effectively, all you've done is add a couple of colons to the classnames...
I honestly hope developers in general don't start using this..."
Let's suppose that your company would do this in java, you create the package com.rs.gizmo and put your classes Bird and Plane into it. Then another developer could stuck other classes into your package.... you see what I mean? it will always be the same thing in php, java c#, c++, etc....
Putting classes in the wrong namespace is not language's fault...
RS
27-Feb-2008 06:31
27-Feb-2008 06:31
I don't see how this implementation of namespaces gives you anything you didn't already have.
Let's say someone is writing a set of classes to handle gizmos... most developers would write something like:
<?php
class GizmoBird() {
}
class GizmoPlane() {
}
?>
Now with namespaces, this would look like:
<?php
namespace Gizmo;
class Bird() {
}
class Plane() {
}
?>
But this doesn't really help you isolate your classes any better. someone else could still write another set of classes with the same namespace, and Gizmo::Bird would clash with another developer's Gizmo::Bird, in the exact same way that GizmoBird would clash with another developer's GizmoBird.
So effectively, all you've done is add a couple of colons to the classnames...
I honestly hope developers in general don't start using this...
someone3x7 at hotmail dot com
20-Feb-2008 05:38
20-Feb-2008 05:38
"but no free code."
Seems can not declare variables in the namespace due to this limitation. Can hardly call it a namespace as is. This with other limitations in php 5.2 sometimes there is need to abuse the global namespace. I'd much rather this issue were resolved rather than ask that php be practically rewritten for true OOP.
