Namespaces Flashcards

1
Q

What two problems are namespaces designed to solve?

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

PHP namespaces provide a way in which to group related…

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
3
Q

What can’t you use as a namespace name?

A

PHP, php, and compound names starting with these words.

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

Where is a namespace declared?

A

At the top of the file, before any code, with one exception: the declare keyword. No non-php code can precede a namespace declaration, not even whitespace.

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

Can multiple namespaces be declared in the same file?

A

Yes. There are two allowed syntaxes, but bracketing the namespaces is the better syntax. However, using multiple namespaces in one file is strongly discouraged.

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

How do you combine global non-namespaced code with namespaced code, in the same file?

A

To do so, only bracketed namespace syntax is supported. Global code should be encased in a namespace statement with no namespace name:

namespace { //global code
$a = 1;
}

No PHP code may exist outside of the namespace brackets, except for an opening declare statement.

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

What are the three ways to which a class can be referred?

A
  1. Unqualified name: $a = new foo();
  2. Qualified name: $a = new subspacename\foo();
  3. Fully qualified name: $a = new \currentnamespace\foo();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you access any global class, function, or constant, within a namespace?

A

A fully qualified name can be used, such as \strlen() or \Exception or \INI_ALL.

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

What are the two ways that PHP supports for abstractly accessing elements within the current namespace?

A

The __NAMESPACE__ magic constant, and the namespace keyword.

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

What is the value of __NAMESPACE__?

A

A string that contains the current namespace name.

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

In global, un-namespaced code, what does __NAMESPACE__ contain?

A

An empty string.

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

The namespace keyword is the namespace equivalent of the ____ operator for classes.

A

The namespace keyword is the namespace equivalent of the self operator for classes.

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

What does this code do?

namespace MyProject
namespace\cname::method();

A

It calls the static method “method” of class MyProject\cname.

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

What is namespace aliasing/importing?

A

The ability to refer to an external fully qualified name with an alias. It’s like a symlink.

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

What are the three kinds of namespace aliasing or importing?

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
16
Q

How is aliasing accomplished in PHP?

A

With the ‘use’ operator.

17
Q

Can you place multiple ‘use’ statements on the same line?

A

Yes, just separate them with commas.

18
Q

When is namespace importing performed?

A

At compile time.

19
Q

Are fully qualified names affected by imports?

A

No, only unqualified and qualified names.

20
Q

When must the ‘use’ keyword be declared?

A

In the outermost scope of a file (the global scope), or inside namespace declarations.

21
Q

Will included files inherit the parent file’s importing rules?

A

No.

22
Q

Class names always resolve to…

A

…the current namespace name.

23
Q

If a namespaced function or constant does not exist, what will PHP do?

A

It will fall back to a global function or constant.

24
Q

For the purposes of namespace resolution rules, define the 3 different namespace names.

A

unqualified name: an identifier without a namespace separator, such as Foo
qualified name: an identifier with a namespace separator, such as Foo\Bar
fully qualified name: an identifier that begins with a namespace separator, such as \Foo\Bar or \Foo

25
Q

What are the 6 namespace resolution rules?

A
  1. Calls to fully qualified functions, classes, or constants are resolved at compile-time. For example 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 import rules (full name substituted for short imported name). For example, if the namespace A\B\C is imported as C, new C() is translated to new A\B\C().
  5. Inside a namespace (say A\B), calls to unqualified functions are resolved at run-time. Here is how a call to function foo() is resolved:
  6. It looks for a function from the current namespace: A\B\foo().
  7. It tries to find and call the global function foo().
  8. Inside a 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():
  9. It looks for a class from the current namespace: A\B\C.
  10. 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.
26
Q

Understand name resolution examples here: http://php.net/manual/en/language.namespaces.rules.php

A

http://php.net/manual/en/language.namespaces.rules.php

27
Q

How are namespace names that contain a backslash (like my\name) but do not begin with a backslash resolved?

A

In 2 different ways:

  1. If there is an import statement that aliases another name to “my”, then the import alias is applied to the “my” in my\name.
  2. Otherwise, the current namespace name is prepended to my\name.
28
Q

How does an unqualified namespace name (one without a backslash), like Foo resolve?

A

In 2 different ways:

  1. If there is an import statement that aliases another name to Foo, then the import alias is applied.
  2. Otherwise, the current namespace name is prepended to Foo.
29
Q

How does an unqualified function name or constant name like Foo resolve?

A

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

  1. First, the current namespace name is prepended to Foo.
  2. Finally, if the constant or function name Foo does not exist in the current namespace, a global constant or function Foo is used if it exists.
30
Q

Does PHP allow nesting namespaces?

A

No.

31
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.

32
Q

What happens when a constant (qualified or unqualified) that contains a backslash, is not found?

A

A fatal error.

33
Q

What happens when you try to define a namespaced constant with the same name as a built-in constant such as NULL, TRUE, FALSE, ZEND_THREAD_SAFE, or ZEND_DEBUG_BUILD?

A

A fatal error.