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

search for in the

namespace sözcüğü ve __NAMESPACE__ sabiti> <İsim alanlarının kullanımı: Temeller
[edit] Last updated: Fri, 23 Mar 2012

view this page in

İsim alanları ve devingen dil özellikleri

PHP'nin isim alanları gerçeklenimi bir programlama dili olarak PHP'nin kendi devingen doğasından etkilenir. Örnek olarak aşağıdaki kodu isim alanlı koda dönüştürmek istersek:

Örnek 1 - Devingen olarak erişilen elemanlar

örnek1.php:

<?php
class sınıfadı
{
    function 
__construct()
    {
        echo 
__METHOD__,"\n";
    }
}
function 
işlevadı()
{
    echo 
__FUNCTION__,"\n";
}
const 
sabitadı "küresel";

$a 'sınıfadı';
$obj = new $a;                   // sınıfadı::__construct basar
$b 'işlevadı';
$b();                            // işlevadı basar
echo constant('sabitadı'), "\n"// küresel basar
?>
Tamamen nitelenmiş isim kullanmak gerekir (isim alanı önekli sınıf adı). Bir devingen sınıf, işlev veya sabit ismi içinde bir nitelenmiş isim ile tamamen nitelenmiş isim arasında bir fark olmadığından baştaki ters bölünün gereksiz oluşuna dikkat edin.

Örnek 2 - Devingen olarak erişilen isim alanlı elemanlar

<?php
namespace isimalanıadı;
class 
sınıfadı
{
    function 
__construct()
    {
        echo 
__METHOD__,"\n";
    }
}
function 
işlevadı()
{
    echo 
__FUNCTION__,"\n";
}
const 
sabitadı "isimalanlı";

include 
'örnek1.php';

$a 'sınıfadı';
$obj = new $a;                    // sınıfadı::__construct basar
$b 'işlevadı';
$b(); // prints funcname
echo constant('sabitadı'), "\n"// küresel basar

/* çift tırnakları kullanacaksanız, "\\isimalanıadı\\sınıfadı"
   biçiminde olmalıdır */
$a '\isimalanıadı\sınıfadı';
$obj = new $a// isimalanıadı\sınıfadı::__construct basar
$a 'isimalanıadı\sınıfadı';
$obj = new $a// bu da isimalanıadı\sınıfadı::__construct basar
$b 'isimalanıadı\işlevadı';
$b(); // isimalanıadı\işlevadı basar
$b '\isimalanıadı\işlevadı';
$b(); //bu da isimalanıadı\işlevadı basan
echo constant('\isimalanıadı\sabitadı'), "\n"// isimalanlı basar
echo constant('isimalanıadı\sabitadı'), "\n"// bu da isimalanlı basar
?>

SSS arasındaki Dizgelerdeki isim alanı adlarının öncelenmesi konusuna da bakmayı unutmayın.



add a note add a note User Contributed Notes İsim alanları ve devingen dil özellikleri
Alexander Kirk 06-Jul-2011 03:57
When extending a class from another namespace that should instantiate a class from within the current namespace, you need to pass on the namespace.

<?php // File1.php
namespace foo;
class
A {
    public function
factory() {
        return new
C;
    }
}
class
C {
    public function
tell() {
        echo
"foo";
    }
}
?>

<?php // File2.php
namespace bar;
class
B extends fooA {}
class
C {
    public function
tell() {
        echo
"bar";
    }
}
?>

<?php
include "File1.php";
include
"File2.php";
$b = new barB;
$c = $b->factory();
$c->tell(); // "foo" but you want "bar"
?>

You need to do it like this:

When extending a class from another namespace that should instantiate a class from within the current namespace, you need to pass on the namespace.

<?php // File1.php
namespace foo;
class
A {
    protected
$namespace = __NAMESPACE__;
    public function
factory() {
       
$c = $this->namespace . '\C';
        return new
$c;
    }
}
class
C {
    public function
tell() {
        echo
"foo";
    }
}
?>

<?php // File2.php
namespace bar;
class
B extends fooA {
    protected
$namespace = __NAMESPACE__;
}
class
C {
    public function
tell() {
        echo
"bar";
    }
}
?>

<?php
include "File1.php";
include
"File2.php";
$b = new barB;
$c = $b->factory();
$c->tell(); // "bar"
?>

(it seems that the namespace-backslashes are stripped from the source code in the preview, maybe it works in the main view. If not: fooA was written as \foo\A and barB as bar\B)
scott at intothewild dot ca 07-Aug-2009 06:33
as noted by guilhermeblanco at php dot net,

<?php

 
// fact.php

 
namespace foo;

  class
fact {

    public function
create($class) {
      return new
$class();
    }
  }

?>

<?php

 
// bar.php

 
namespace foo;

  class
bar {
  ...
  }

?>

<?php

 
// index.php
 
 
namespace foo;

  include(
'fact.php');
 
 
$foofact = new fact();
 
$bar = $foofact->create('bar'); // attempts to create \bar
                                  // even though foofact and
                                  // bar reside in \foo

?>
guilhermeblanco at php dot net 16-Jun-2009 03:04
Please be aware of FQCN (Full Qualified Class Name) point.
Many people will have troubles with this:

<?php

// File1.php
namespace foo;

class
Bar { ... }

function
factory($class) {
    return new
$class;
}

// File2.php
$bar = foofactory('Bar'); // Will try to instantiate \Bar, not \foo\Bar

?>

To fix that, and also incorporate a 2 step namespace resolution, you can check for \ as first char of $class, and if not present, build manually the FQCN:

<?php

// File1.php
namespace foo;

function
factory($class) {
    if (
$class[0] != '\\') {
        echo
'->';
        
$class = '\\' . __NAMESPACE__ . '\\' . $class;
    }

    return new
$class();
}

// File2.php
$bar = foofactory('Bar'); // Will correctly instantiate \foo\Bar

$bar2 = foofactory('\anotherfoo\Bar'); // Wil correctly instantiate \anotherfoo\Bar

?>

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