PHP Doku:: Namespaces und dynamische Sprachfeatures - language.namespaces.dynamic.html

Verlauf / Chronik / History: (50) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchSprachreferenzNamespacesNamespaces und dynamische Sprachfeatures

Ein Service von Reinhard Neidl - Webprogrammierung.

Verdiene Geld mit Deiner Homepage oder deinem Blog: Setzte eine Textlinkwerbung und bestimme den Preis selber.
Einfach kostenlos anmelden und einen Platz auf Deiner Homepage anbieten.
Make money with your homepage or blog: Set a text link advertising and declare the price.
Register free of charge and offer a place on your homepage.
Namespaces

<<Namespaces verwenden: Grundlagen

Namespace-Schlüsselwort und __NAMESPACE__-Konstante>>

Namespaces und dynamische Sprachfeatures

Die Implementierung von Namespaces in PHP ist stark von seinen Eigenschaften als Programmiersprache mit dynamischen Features beeinflusst. Man kann also den folgenden Code in Code mit Namespaces umformen:

Beispiel #1 Dynamischer Zugriff auf Elemente

example1.php:

<?php
class classname
{
    function 
__construct()
    {
        echo 
__METHOD__,"\n";
    }
}
function 
funcname()
{
    echo 
__FUNCTION__,"\n";
}
const 
constname "global";

$a 'classname';
$obj = new $a// gibt classname::__construct aus
$b 'funcname';
$b(); // gibt funcname aus
echo constant('constname'), "\n"// gibt global aus
?>
Um diesen Code umzuformen, muss man den vollständig qualifizierten Namen (Klassenname mit namespace-Präfix) verwenden. Beachten Sie, dass, weil es keinen Unterschied zwischen einem qualifizierten und vollständig qualifizierten Namen innerhalb eines dynamischen Klassen-, Funktions- oder Konstantennamen gibt, der führende Backslash nicht notwendigerweise angegeben werden muss.

Beispiel #2 Dynamischer Zugriff auf Elemente mit Namespace

<?php
namespace namespacename;
class 
classname
{
    function 
__construct()
    {
        echo 
__METHOD__,"\n";
    }
}
function 
funcname()
{
    echo 
__FUNCTION__,"\n";
}
const 
constname "namespaced";

include 
'example1.php';

$a 'classname';
$obj = new $a// gibt classname::__construct aus
$b 'funcname';
$b(); // gibt funcname aus
echo constant('constname'), "\n"// gibt global aus

/* Wenn man doppelte Anführungszeichen verwendet,
muss "\\namespacename\\classname" verwendet werden */
$a '\namespacename\classname';
$obj = new $a// gibt namespacename\classname::__construct aus
$a 'namespacename\classname';
$obj = new $a// gibt ebenfalls namespacename\classname::__construct aus
$b 'namespacename\funcname';
$b(); // gibt namespacename\funcname aus
$b '\namespacename\funcname';
$b(); // gibt ebenfalls namespacename\funcname aus
echo constant('\namespacename\constname'), "\n"// gibt namespaced aus
echo constant('namespacename\constname'), "\n"// gibt ebenfalls namespaced aus
?>

Bitte lesen Sie auch den Hinweis zum Escaping von Namespacenamen in Strings.


Verdiene Geld mit Deiner Homepage oder deinem Blog: Setzte eine Textlinkwerbung und bestimme den Preis selber.
Einfach kostenlos anmelden und einen Platz auf Deiner Homepage anbieten.
Make money with your homepage or blog: Set a text link advertising and declare the price.
Register free of charge and offer a place on your homepage.
2 BenutzerBeiträge:
- Beiträge aktualisieren...
scott at intothewild dot ca
8.08.2009 0: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.06.2009 21: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

?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)