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

search for in the

Küresel Alan> <namespace sözcüğü ve __NAMESPACE__ sabiti
[edit] Last updated: Fri, 23 Mar 2012

view this page in

İsim alanlarının kullanımı: İthal/Rumuz

Dış kaynaklı tamamen nitelenmiş bir isme bir takma adla veya ithal ederek başvurma yeteneği isim alanlarının önemli bir özelliğidir. Bu özellik, Unix dosya sistemlerinin bir dizin veya dosyaya bir sembolik bağ oluşturma yeteneğine benzer.

PHP isim alanları iki çeşit takma ad kullanımını veya ithalini destekler: bir sınıf adına takma ad ve bir isim alanı adına takma ad. Bir işlevin veya sabitin ithalinin desteklenmediğine dikkat ediniz.

PHP'da takma ad kullanımı use işleci üzerinden sağlanır. Aşağıda 3 çeşit ithal örneğine yer verilmiştir:

Örnek 1 - use işleci ile ithal/rumuz kullanımı

<?php
namespace fan;
use 
Bir\Tam\Sınıfadı as BirBaşkası;

// use Bir\Tam\iAadı as iAadı ile aynıdır
use Bir\Tam\iAadı;

// küresel bir sınıfın ithali
use \DiziNesnesi;

$nesne = new namespace\BirBaşkası// fan\BirBaşkası sınıfını örnekler
$nesne = new BirBaşkası// Bir\Tam\Sınıfadı  sınıfını örnekler
iAadı\altia\işlev(); // Bir\Tam\iAadı\altia\işlev işlevini çağırır
$a = new DiziNesnesi(array(1)); // DiziNesnesi sınıfını örnekler
// "use \DiziNesnesi" olmasaydı fan\DiziNesnesi sınıfını örneklerdik
?>
İsim alanlı adlar için baştaki terbölü gereksiz olup izin verilmediği gibi bunlar geçerli isim alanına göre işlenmezler. İthal edilen isimler ise tamamen nitelenmiş olmalıdır. (Tamamen nitelenmiş isimler Fan\Fin şeklinde isim alanı ayracı içermelerine karşın küresel isimler FanFin şeklinde isim alanı ayracı içermezler.)

PHP bunlara ek olarak çok sayıda use deyiminin aynı satırda kullanılmasına imkan veren bir kısayola da sahiptir.

Örnek 2 - use işleci ile rumuz/ithal, çoklu use kullanımı

<?php
use Bir\Tam\Sınıfadı as BirBaşkasıBir\Tam\iAadı;

$nesne = new BirBaşkası// Bir\Tam\Sınıfadı sınıfını örnekler
iAadı\altia\işlev();     // Bir\Tam\iAadı\altia\işlev işlevini çağırır
?>

İthal işlemi derleme sırasında yerine getirilir ve dolayısıyla devingen sınıf, işlev ve sabit isimlerini etkilemez.

Örnek 3 - İthal işlemi ve devingen isimler

<?php
use Bir\Tam\Sınıfadı as BirBaşkasıBir\Tam\iAadı;

$nesne = new BirBaşkası// Bir\Tam\Sınıfadı sınıfını örnekler
$a 'BirBaşkası';
$nesne = new $a;         // BirBaşkası sınıfını örnekler
?>

Bunlara ek olarak, ithal işlemi sadece nitelenmemiş ve nitelenmiş isimleri etkiler. Tamamen nitelenmiş isimler mutlak olup ithal işleminden etkilenmez.

Örnek 4 - İthal işlemi ve tamamen nitelenmiş isimler

<?php
use Bir\Tam\Sınıfadı as BirBaşkasıBir\Tam\iAadı;

$nesne = new BirBaşkası;       // Bir\Tam\Sınıfadı sınıfını örnekler
$nesne = new \BirBaşkası;        // BirBaşkası sınıfını örnekler
$nesne = new BirBaşkası\birşey;  // Bir\Tam\Sınıfadı\birşey sınıfını örnekler
$nesne = new \BirBaşkası\birşey// BirBaşkası\birşey sınıfını örnekler
?>



add a note add a note User Contributed Notes İsim alanlarının kullanımı: İthal/Rumuz
samuel dot roze at gmail dot com 11-May-2012 03:34
(All the backslashes in namespaces are slashes because I can't figure out how to post backslashes here.)

You can have the same "use" for a class and a namespace. For example, if you have these files:

<?php
// foo/bar.php
namespace foo;

class
bar
{
    public function
__toString ()
    {
        return
'foo\bar\__toString()';
    }
}
?>

<?php
// foo/bar/MyClass.php
namespace foo/bar;

class
MyClass
{
    public function
__toString ()
    {
        return
'foo\bar\MyClass\__toString()';
    }
}
?>

In another namespace, you can do:
<?php
namespace another
;
require_once
'foo/bar.php';
require_once
'foo/bar/MyClass.php';

use
foo/bar;

$bar = new bar();
echo
$bar."\n";

$class = new bar/MyClass();
echo
$class."\n";
?>

And it will makes the following output:
foo\bar\__toString()
foo\bar\MyClass\__toString()
c dot 1 at smithies dot org 14-Aug-2011 05:26
If you are testing your code at the CLI, note that namespace aliases do not work!

(Before I go on, all the backslashes in this example are changed to percent signs because I cannot get sensible results to display in the posting preview otherwise. Please mentally translate all percent signs henceforth as backslashes.)

Suppose you have a class you want to test in myclass.php:

<?php
namespace my
%space;
class
myclass {
 
// ...
}
?>

and you then go into the CLI to test it. You would like to think that this would work, as you type it line by line:

require 'myclass.php';
use my%space%myclass; // should set 'myclass' as alias for 'my%space%myclass'
$x = new myclass; // FATAL ERROR

I believe that this is because aliases are only resolved at compile time, whereas the CLI simply evaluates statements; so use statements are ineffective in the CLI.

If you put your test code into test.php:
<?php
require 'myclass.php';
use
my%space%myclass;
$x = new myclass;
//...
?>
it will work fine.

I hope this reduces the number of prematurely bald people.
Jan Tvrdk 11-Jan-2011 07:26
Importing and aliasing an interface name is also supported.
thinice at gmail.com 01-Dec-2010 11:07
Because imports happen at compile time, there's no polymorphism potential by embedding the use keyword in a conditonal.

e.g.:

<?php
if ($objType == 'canine') {
  use
AnimalCanine as Beast;
}
if (
$objType == 'bovine') {
  use
AnimalBovine as Beast;
}

$oBeast = new Beast;
$oBeast->feed();
?>
nsdhami at live dot jp 15-Jul-2010 04:01
The "use" keyword can not be declared inside the function or method. It should be declared as global, after the "namespace" as:

<?php

namespace mydir
;

// works perfectly
use mydir/subdir/Class1 as Class1;

function
fun1()
{
   
// Parse error: syntax error, unexpected T_USE
   
use mydir/subdir/Class1 as Class1;
}

class
Class2
{
    public function
fun2()
    {
       
// Parse error: syntax error, unexpected T_USE
       
use mydir/subdir/Class1 as Class1;
    }
}
?>

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