SheppardDigital
asked on
PHP Namespace
Hi,
I have some questions on namespaces place.
I have the following class;
I then have classes that extends the above class
Do I have to also add the namespace to the class that's extending the first class? or does it inherit the namespace from the first class? I'm guessing I have to define the namespace?
My second question. I have a helper class;
I then have a view class;
If I want to access methods in the Html class from within the include file, do I have to supply the full namespace? i.e. \Digital\Helper\Url() I thought because the include file is within the View class, I'd be able to simply use Helper\Url() but it doesn't seem to work.
I have some questions on namespaces place.
I have the following class;
namespace /Digital/Model
class Base {
}
I then have classes that extends the above class
use \Digital\Model;
class car extends Model/Base {
}
Do I have to also add the namespace to the class that's extending the first class? or does it inherit the namespace from the first class? I'm guessing I have to define the namespace?
My second question. I have a helper class;
namespace \Digital\Helper;
class Html {
}
I then have a view class;
uses \Digital\Helper;
class View {
public function includeFile() {
// Includes a PHP file here
// The include is plain html with a bit of PHP
}
}
If I want to access methods in the Html class from within the include file, do I have to supply the full namespace? i.e. \Digital\Helper\Url() I thought because the include file is within the View class, I'd be able to simply use Helper\Url() but it doesn't seem to work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just think of the "namespace" statement as a virtual "folder" for your files. If you put that namespace at the top of your file, then it moves that file into that particular virtual folder along with everything else that might belong inside of it. If it's in the same folder / namespace as another file, then the two files can see each other directly - they don't need to spell out the full path each time.
Similarly, anything OUTSIDE of that namespace / folder needs to declare that path when making a reference to any file inside of it.
Similarly, anything OUTSIDE of that namespace / folder needs to declare that path when making a reference to any file inside of it.
ASKER
@gr8gonzo
Thanks, that explains what I needed.
1) The slashes, it was late, working on a laptop and kept getting the wrong slash.
2) So by simple making the file part of the namespace then I don't need to use the 'use' clause, because I'm already in the namespace. Hence the class extension doesn't need the namespace in it.
3) I was thinking about this after I posted this question, but I was in bed and couldn't test. But I get it now, namespaces should be defined per file.
4) Answered by 3)
Thanks, that explains what I needed.
1) The slashes, it was late, working on a laptop and kept getting the wrong slash.
2) So by simple making the file part of the namespace then I don't need to use the 'use' clause, because I'm already in the namespace. Hence the class extension doesn't need the namespace in it.
3) I was thinking about this after I posted this question, but I was in bed and couldn't test. But I get it now, namespaces should be defined per file.
4) Answered by 3)
On #2 - the class extension DOES need the namespace if you want the class extension to be part of that same namespace (which is usually the case). If you don't declare a namespace at the top of the file, then all of the code in that particular file is not inside a namespace.
Or, who knows? Maybe another Expert will come along with a magic bullet!
All the best, ~Ray