Namespaces Flashcards

1
Q

What two problems are namespaces designed to solve in PHP?

A
  1. Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
  2. Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are namespaces in PHP?

A

A way to group related classes, interfaces, functions and constants.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What namespace names are reserved for internal language?

A

Namespace names PHP and php, and compound names starting with these names (like PHP\Classes).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which types of code are affected by namespaces?

A

Classes (including abstracts and traits), interfaces, functions and constants.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How are namespaces declared?

A

With the “namespace” keyword. A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the “declare” keyword, for defining encoding of a source file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can non-php code precede a namespace declaration?

A

No. Not even extra whitespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can the same namespace be defined in multiple files?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can namespaces be defined with sublevels, to specify a hierarchy of namespace names?

A

Yes, like this:

namespace MyProject\Sub\Level;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can multiple namespaces be declared in the same file?

A

Yes. There are two allowed syntaxes. With one you just declare multiple namespaces in a file. With the other, recommended method, you declare it like a function, with the namespace name followed by a braced section of code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can global non-namespaced code be combined with namespaced code in a single file?

A

By using bracketed syntax. Global code should be enclosed in a namespace statement with no namespace name, just the “namespace” keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Regarding namespaces, what are the three ways that a class can be referred to in PHP?

A
  1. Unqualified name: $a = new foo() or foo::staticmethod(). If the current namespace is currentnamespace, this resolves to currentnamespace\foo.
  2. Qualified name: $a = new subnamespace\foo() or subnamespace\foo::staticmethod(). If the current namespace is currentnamespace, this resolves to currentnamespace\subnamespace\foo.
  3. Fully qualified name: $a = new \currentnamespace\foo() or \currentnamespace\foo::staticmethod(). This always resolves to the literal name specified in the code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you access global classes, functions, or constants from within a namespace?

A

Use a fully qualified name, like \strlen() or \INI_ALL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the value of __NAMESPACE__?

A

A string that contains the current namespace name. In global, un-namespaced code, it contains an empty string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the three kinds of aliasing or importing that PHP supports with namespaces?

A

Aliasing a class name, aliasing an interface name, and aliasing a namespace name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is aliasing accomplished?

A

With the “use” operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

For fully qualified namespace names, is the leading backslash necessary?

A

No, and it’s not recommended. Import names must be fully qualified and are not processed relative to the current namespace.

17
Q

Can multiple use statements be placed on the same line?

A

Yes, separated by commas.

18
Q

Are fully qualified names affected by importing?

A

No, fully qualified names are absolute, and unaffected by imports.

19
Q

Where must the use keyword be declared?

A

In the outermost scope of a file (the global scope), or inside namespace declarations. This is because importing is done at compile time, not runtime, so it cannot be block scoped.

20
Q

Without any namespace definition, in what scope are class and function definitions?

A

Global.

21
Q

What does prefixing a name with \ do?

A

Requires that it be called from the global space, even in the context of a namespace.

22
Q

Do class names resolve globally, or to the current namespace name?

A

To the current namespace name. So they will draw a fatal error if not found. However, functions and constants will fall back to global if a namespaced equivalent does not exist.

23
Q

What is an unqualified namespace name?

A

An identifier without a namespace separator: Foo.

24
Q

What is a qualified namespace name?

A

An identifier with a namespace separator: Foo\Bar.

25
Q

What is a fully qualified name?

A

An identifier that begins with a namespace separator, such as \Foo\Bar. The namespace \Foo is also a fully qualified name.

26
Q

What are the name resolution rules?

A
  1. Calls to fully qualified functions, classes or constants are resolved at compile-time. For instance new \A\B resolves to class A\B.
  2. All unqualified and qualified names (not fully qualified names) are translated during compilation according to current import rules. For example, if the namespace A\B\C is imported as C, a call to C\D\e() is translated to A\B\C\D\e().
  3. Inside a namespace, all qualified names not translated according to import rules have the current namespace prepended. For example, if a call to C\D\e() is performed within namespace A\B, it is translated to A\B\C\D\e().
  4. Unqualified class names are translated during compilation according to current import rules (full name substituted for short imported name). In example, if the namespace A\B\C is imported as C, new C() is translated to new A\B\C().
  5. Inside namespace (say A\B), calls to unqualified functions are resolved at run-time. Here is how a call to function foo() is resolved:
    1. It looks for a function from the current namespace: A\B\foo().
    2. It tries to find and call the global function foo().
  6. Inside namespace (say A\B), calls to unqualified or qualified class names (not fully qualified class names) are resolved at run-time. Here is how a call to new C() or new D\E() is resolved. For new C():
    1. It looks for a class from the current namespace: A\B\C.
    2. It attempts to autoload A\B\C.

For new D\E():

1. It looks for a class by prepending the current namespace: A\B\D\E.
2. It attempts to autoload A\B\D\E.
27
Q

How does a namespace name like my\name resolve?

A

Names that contain a backslash but do not begin with a backslash like my\name can be resolved in 2 different ways.

If there is an import statement that aliases another name to my, then the import alias is applied to the my in my\name.

Otherwise, the current namespace name is prepended to my\name.

28
Q

How does an unqualified class name like “name” resolve?

A

Class names that do not contain a backslash like name can be resolved in 2 different ways.

If there is an import statement that aliases another name to name, then the import alias is applied.

Otherwise, the current namespace name is prepended to name.

29
Q

How does an unqualified function name or unqualified constant name like “name” resolve?

A

Function or constant names that do not contain a backslash like name can be resolved in 2 different ways.

First, the current namespace name is prepended to name.

Finally, if the constant or function name does not exist in the current namespace, a global constant or function name is used if it exists.

30
Q

Can functions or constants be imported via the “use” statement?

A

No. The only elements that are affected by “use” statements are namespaces and class names.

31
Q

Can namespaces be used to override special constants: NULL, TRUE, FALSE, etc.?

A

No.