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.
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array
definition as it guarantees a stable state of the code.
Let’s take a look at an example:
foreach($collectionas$item){$myArray['foo']=$item->getFoo();if($item->hasBar()){$myArray['bar']=$item->getBar();}// do something with $myArray}
As you can see in this example, the array $myArray is initialized the first
time when the foreach loop is entered. You can also see that the value of the
bar key is only written conditionally; thus, its value might result from a
previous iteration.
This might or might not be intended. To make your intention clear, your code
more readible and to avoid accidental bugs, we recommend to add an explicit
initialization $myArray=array() either outside or inside the foreach loop.
The variable $res does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined
for all execution paths.
Let’s take a look at an example:
functionmyFunction($a){switch($a){case'foo':$x=1;break;case'bar':$x=2;break;}// $x is potentially undefined here.echo$x;}
In the above example, the variable $x is defined if you pass “foo” or “bar”
as argument for $a. However, since the switch statement has no default
case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
functionmyFunction($a){switch($a){case'foo':$x=1;break;case'bar':$x=2;break;}if(isset($x)){// Make sure it's always set.echo$x;}}
Define a default value for the variable:
functionmyFunction($a){$x='';// Set a default which gets overridden for certain paths.switch($a){case'foo':$x=1;break;case'bar':$x=2;break;}echo$x;}
Add a value for the missing path:
functionmyFunction($a){switch($a){case'foo':$x=1;break;case'bar':$x=2;break;// We add support for the missing case.default:$x='';break;}echo$x;}
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 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: