• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 67
  • Last Modified:

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.
0
SheppardDigital
Asked:
SheppardDigital
  • 3
1 Solution
 
Ray PaseurCommented:
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
0
 
gr8gonzoConsultantCommented:
Couple things:

1. Don't use a starting slash, and the slashes should all be backslashes:
namespace Digital\Model;

Open in new window


2. The "use" syntax is for aliases. It doesn't really belong in the above code that you have. Instead, your second snippet should simply repeat:
namespace Digital\Model;

Open in new window

...at the top. This will make your "car" class definition belong to that namespace, the same one as your Base class. This lets your "car" class find the right class to extend. So your code should probably look like:

<?php
namespace Digital\Model;

class car extends Base
{
}

Open in new window


3. The namespace declaration is per-file and should always be at the TOP of your file.

4.
If I want to access methods in the Html class from within the include file, do I have to supply the full namespace?
Yes, UNLESS your include file has the same namespace declaration. It doesn't really matter that your include() is within a class or within another file that has a different namespace. Ultimately, when you include() that 2nd file, that 2nd file is basically running like any other normal include. It doesn't automatically inherit any namespaces, nor does it add anything to the class definition.

So if you included a file that just said:
<?php
function CreateNewHtmlObject()
{
  $object = new Digital\Helper\Html();
  return $object;
}

Open in new window


...then the function CreateNewHtmlObject() would be accessible without any scope at all. You could call it from anywhere. Or you could do:

<?php
namespace Digital\Helper;

function CreateNewHtmlObject()
{
  $object = new Html();
  return $object;
}

Open in new window


...but then you would have to call this function like:
$my_html_object = Digital\Helper\CreateNewHtmlObject();

Make sense?
0
 
gr8gonzoConsultantCommented:
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.
0
 
SheppardDigitalAuthor Commented:
@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)
0
 
gr8gonzoConsultantCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now