and let’s assume the following content of Bar.php:
// Bar.phpnamespaceOtherDir;useSomeDir\Foo;// This now conflicts the class OtherDir\Foo
If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the
same runtime, you will see a PHP error such as the following:
PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as OtherDir/Foo.php does not necessarily have to be loaded and the
error is only triggered if it is loaded before OtherDir/Bar.php, this problem
might go unnoticed for a while. In order to prevent this error from surfacing,
you must import the namespace with a different alias:
// Bar.phpnamespaceOtherDir;useSomeDir\FooasSomeDirFoo;// There is no conflict anymore.
Loading history...
9
10
/**
11
* Render a view file with php.
12
*/
13
class PhpView implements ViewInterface {
14
use HasContextTrait;
15
16
/**
17
* View name.
18
*
19
* @var string
20
*/
21
protected $name = '';
22
23
/**
24
* Filepath to view.
25
*
26
* @var string
27
*/
28
protected $filepath = '';
29
30
/**
31
* {@inheritDoc}
32
*/
33
1
public function getName() {
34
1
return $this->name;
35
}
36
37
/**
38
* {@inheritDoc}
39
*/
40
1
public function setName( $name ) {
41
1
$this->name = $name;
42
1
return $this;
43
}
44
45
/**
46
* Get filepath.
47
*
48
* @return string
49
*/
50
public function getFilepath() {
51
return $this->filepath;
52
}
53
54
/**
55
* Set filepath.
56
*
57
* @param string $filepath
58
* @return self $this
59
*/
60
public function setFilepath( $filepath ) {
61
$this->filepath = $filepath;
62
return $this;
63
}
64
65
/**
66
* {@inheritDoc}
67
*/
68
7
public function toString() {
69
7
if ( empty( $this->getName() ) ) {
70
1
throw new Exception( 'View must have a name.' );
71
}
72
73
6
if ( empty( $this->getFilepath() ) ) {
74
1
throw new Exception( 'View must have a filepath.' );
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: