downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Alt İsim Alanlarının Bildirilmesi> <Giriş
[edit] Last updated: Fri, 23 Mar 2012

view this page in

İsim Alanlarının Tanımlanması

İsim alanları içinde her çeşit PHP kodu bulunabilirse de sadece üç tür kod isim alanlarından etkilenir: Sınıflar, işlevler ve sabitler.

İsim alanları, namespace anahtar sözcüğü ile bildirilirler. Bir isim alanını içeren bir dosyada isim alanı bütün diğer kodlardan önce (ama bir declare satırından önce değil) dosyanın başlarında bildirilmelidir.

Örnek 1 - Tek bir isim alanı bildirimi

<?php
namespace Projem;

const 
BAĞLANTI_TAMAM 1;
class 
Bağlantı /* ... */ }
function 
bağlan() { /* ... */  }

?>
Bir isim alanı bildiriminden önce bulunmasına izin verilen tek oluşum, kaynak dosyanın kodlamasının tanımlandığı declare deyimidir. Bunun dışında, baştaki fazladan boşluklar da dahil hiçbir PHP-dışı kod, bir isim alanı bildiriminden önce yer alamaz. Örnek:

Örnek 2 - Tek bir isim alanı bildirimi

<html>
<?php
namespace Sınıfım// ölümcül hata - namespace, betikteki ilk deyim olmalıdır
?>

Diğer tüm PHP oluşumlarının tersine, isim alanının dosya sistemi içinde parçalara bölünebilmesi için aynı isim alanı birden fazla dosyada tanımlanabilir.



Alt İsim Alanlarının Bildirilmesi> <Giriş
[edit] Last updated: Fri, 23 Mar 2012
 
add a note add a note User Contributed Notes İsim Alanlarının Tanımlanması
huskyr at gmail dot com 05-Oct-2009 06:20
"A file containing a namespace must declare the namespace at the top of the file before any other code"

It might be obvious, but this means that you *can* include comments and white spaces before the namespace keyword.

<?php
// Lots
// of
// interesting
// comments and white space

namespace Foo;
class
Bar {
}
?>
jeremeamia at gmail dot com 14-Jul-2009 10:43
You should not try to create namespaces that use PHP keywords. These will cause parse errors.

Examples:

<?php
namespace Project
/Classes/Function; // Causes parse errors
namespace Project/Abstract/Factory; // Causes parse errors
?>
danbettles at yahoo dot co dot uk 14-Apr-2009 02:02
Regarding constants defined with define() inside namespaces...

define() will define constants exactly as specified.  So, if you want to define a constant in a namespace, you will need to specify the namespace in your call to define(), even if you're calling define() from within a namespace.  The following examples will make it clear.

The following code will define the constant "MESSAGE" in the global namespace (i.e. "\MESSAGE").

<?php
namespace test
;
define('MESSAGE', 'Hello world!');
?>

The following code will define two constants in the "test" namespace.

<?php
namespace test
;
define('test\HELLO', 'Hello world!');
define(__NAMESPACE__ . '\GOODBYE', 'Goodbye cruel world!');
?>
David Drakard 07-Sep-2008 07: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 14-May-2008 02: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.
Anonymous 01-Apr-2008 01: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.

 
show source | credits | sitemap | contact | advertising | mirror sites