Link to home
Start Free TrialLog in
Avatar of SheppardDigital
SheppardDigital

asked on

PHP Namespace

Hi,

I have some questions on namespaces place.

I have the following class;

namespace /Digital/Model

class Base {
}

Open in new window


I then have classes that extends the above class

use \Digital\Model;

class car extends Model/Base {
}

Open in new window


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 {
}

Open in new window


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
}

}

Open in new window


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.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Namespaces are well-aligned with auto-loaders and can generally be thought of the same way you might think of a file directory structure.  I'm currently writing an article on PHP namespaces because we are working through namespaces for an Army Laravel application.  It may take me a while to get you a good answer here, since I'm doing the research as I go into the article, but if I can shed some light on it, I will.  If Monday comes and there is no answer yet, you might want to delete this question (obviously I won't object) and repost it so that you can get some new eyes on the topic!

Or, who knows?   Maybe another Expert will come along with a magic bullet!

All the best, ~Ray
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of SheppardDigital
SheppardDigital

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