PHP Name Resolution Rules


Introduction

In a PHP code, appearance of namespace is resolved subject to following rules −

  • A namespace identifier without namespace separator symbol (/) means it is referring to current namespace. This is an unqualified name.

  • If it contains separator symbol as in myspace\space1, it resolves to a subnamespace space1 under myspace. Such type of naming is relative namespace.

  • Name of fully qualified namespace starts with \ character. For example, \myspace or \myspace\space1.

  • Fully qualified names resolve to absolute namespace. For example \myspace\space1 resolves to myspace\space1 namespace

  • If the name occurs in the global namespace, the namespace\ prefix is removed. For example namespace\space1 resolves to space1.

  • However, if it occurs inside another namespace, it is treated differently. For example, if namespace\space1 is inside myspace, it is equivalent to myspace\space1

  • First segment of the name in qualified name is translated according to the current class/namespace import table.

  • If no import rule applies, the current namespace is prepended to the name.

  • class-like names are translated according to the class/namespace import table, function names according to the function import table and constants according to the constant import table.

  • For unqualified names, if no import rule applies and the name refers to a function or constant and the code is outside the global namespace, the name is resolved at runtime. First it looks for a function from the current namespace, then it tries to find and call the global function.

Updated on: 18-Sep-2020

144 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements