CuriousMind
CuriousMind

Reputation: 34145

PHP 5.3 namespaces in Symfony 2

I am getting started with Symfony 2 & I am following this tutorial. Now, this tutorial is easy & I was able to follow it completely. This is the code in my Controller

<?php
namespace DemoCompany\HelloBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

class HelloController{
    public function indexAction($name){
        return new Response('<h1>hello ' . ucfirst($name) . '</h1>');
    }
}

Now, I have some questions

  1. When I run the code without including namespace DemoCompany\HelloBundle\Controller;, it gives me an error. But it does work fine when this line is included; so I want to know: Why does it work with namespace statement & why doesn't it work without namespace. Please clarify.

  2. Follow up question: I am returning a new Response object but it is neither being defined, nor is my class HelloController extending some baseclass (which may have this response object defined). So the question is: How is PHP able to find & then load this file?

  3. In Symfony, do custom controllers/models/views need to extend some base class? For example, in Codeigniter, we have to do something like class Blog extends CI_Controller, so that all the methods defined in CI_Controller would be available in current scope using $this. Does Symfony have the same practice or does it do something different?

  4. Since this controller is namespaced to DemoCompany\HelloBundle\Controller, how come normal PHP functions work without a backslash?

Upvotes: 2

Views: 8479

Answers (2)

Josh
Josh

Reputation: 1281

I've only very briefly used Symfony2, here are my answers though:

1) That'll be because Symfony needs your Controller to exist in the DemoCompany\HelloBundle\Controller namespace for autoloading

2) At use Symfony\Component\HttpFoundation\Response;, you're telling PHP to look in there if the method can't be found locally

3) Don't know on that one...

4) Built-in PHP functions aren't in any specific namespace (namespaces are new to PHP), so work anywhere

Upvotes: 2

Roberto
Roberto

Reputation: 1944

1: Namespaces act as 'folders' of sorts, with which you can encapsulate items to avoid naming collisions. You should go ahead and read all of PHP Documentation on the matter: Namespaces overview. Not including it means that you are using the global namespace, or \, while using it tells PHP that everything in the file belongs to the declared namespace, such that the fully qualified name for HelloController becomes DemoCompany\HelloBundle\Controller\HelloController

2: The Request class was actually defined when you use Symfony\Component\HttpFoundation\Response. This declaration tells PHP and Symfony to look for that class, probably using spl_register_autoloader, and include it in the current namespace, so that you can use the short name Response instead of the FQN.

4: PHP actually does a lookup for every class/function you call, searching first in the declared namespace, and then moving up to the global namespace. You should really read the documentation, it helped me out a lot while learning about these relatively new features (at least for PHP)

On #3, I have no idea, but I suppose that you can chose not to extend any other base class, should you choose to do so. Of course, this will not let your code inherit the methods set up by the parent classes, and you would have to implement those yourself. For example, not extending the base Model class would make you set up your connection to the database and such.

Hope this helps.

Upvotes: 5

Related Questions