I was trying to understand use of Aliasing/Importing in php namespaces from php.net/manual sample example. I slightly modified the sample example to explore all the features of php namespace Aliasing/Importing in a single code file. I also added multiple namespaces in the same file to observe all the features. Here is the php source code:
<?php
namespace foo{
echo "1-Inside namespace foo
";
use My\Full\Classname as Another;
use ArrayObject; // importing a global class
echo "2-After using ArrayObject line: ".__LINE__."
";
class Another {
public function cname() {
echo "line".__LINE__."Method".__METHOD__."
";
}
static function method(){echo "line".__LINE__."Method: ".__METHOD__."
";}
}
//This line below is Not working.
$obj1 = new namespace\Another; // instantiates object of foo\Another
echo "3-After namespace\Another object line: ".__LINE__."
";
//This line below is Not working.
echo "4-".$obj1->method()."
";
//This line below is Not working.
$obj2 = new Another; // instantiates object of class My\Full\Classname
echo "5-After Another object line: ".__LINE__."
";
//This line below is Not working.
echo "6-".$obj2->method()."
";
echo "7-After Another object line: ".__LINE__."
";
//This line below is Not working.
$a = new ArrayObject(array(1)); // instantiates object of ArrayObject
echo "8-After ArrayObject line: ".__LINE__."
";
}
namespace My\Full{
class Classname{
public function cname() {
echo "line".__LINE__."Method".__METHOD__."
";
}
static function method(){echo "line".__LINE__."Method: ".__METHOD__."
";} } }
?>
I expected & wanted these desired behavior/output to be printed given below:
1-Inside namespace foo
2-After using ArrayObject line: x
3-After namespace\Another object line: x
4-line: x Method: foo\Another:method()
5-After Another object line: x
6-line: x Method: My\Full\Classname:method()
7-After Another object line: x
8-After ArrayObject line: x
But Instead of desired output, I got only few outputs with errors given below:
PHP Fatal error: Cannot declare class foo\Another because the name is already in use in /home/himadree/ php workspace/ZCE/TOPIC1:BASICS/Namespaces/Using-namespaces-Aliasing-Importing.php on line 8
So, it looks like there might be any identical naming conflicts with the class name Another Moreover i noticed that class 'Another' has 2 versions: foo\Another & My\Full\Classname as Another.After some useful comments i added extra class'Another'within namespace foo.But still its not working.Is this any the only error! i don't know. Therefore in order to get my desired behavior/output, what changes are needed in this code? I used eclipse php pdt tools, php version 5.5. Since it is debug issue, so let me know if more specific information required.Thanks
The specific problem is that there is no class foo/Another
defined. You're missing this within namespace foo
:
class Another {}
Without it or something like it, foo\Another
does not exist and attempting to instantiate it, as you are, will lead to a fatal error, as it does.
Moreover i noticed that class 'Another' has 2 versions: foo\Another & My\Full\Classname as Another.
That's the point this code is trying to demonstrate. Just Another
would be ambiguous, since it could refer to the aliased as Another
or to foo\Another
. Hence why namespace\Another
is used to explicitly refer to foo\Another
. As opposed to the new Another
, which refers to the aliased as Another
.