Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

\ in OOP PHP

Hi @hamptonpaulk could you explain why you use \ in OOP PHP an example of this would be in Silex where they have the following:

$app = new Silex\Application();

So thats's saying create a new Silex\Application object but like I say why use a \ ?

3 Answers

Hi Gareth ,

The back slash i.e. " \ " is used for Namespaces . So the code snippet u have written means that you want to create a new application which is in the namespace of Silex.. Here is the link that you would find useful about namespaces http://www.php.net/manual/en/language.namespaces.rationale.php

hope that helps. Cheers Ali

Ahh thank you Muhammad that helps. So I am not sure if you have watched the Silex Workshop but from 14 mins in it talks about creating Application within the Silex namespace. If I look within the folder structure of Silex there is the Aplication.php file in the silex folder. My question is would that be why you then use the namespace as

$app = new Silex\Application();

Yes you are thinking in the right direction. Although I am a learner myself but from what i understand about namespaces is that it there to make sure that there is no conflict in file names. imagine if you had two files exactly the same name as Application.php with the Application class.. If you instantiate the classes after requiring them in another php file... PHP wont be able to understand the difference as which class are you trying to instantiate. But if you provide namespaces then you can easily instantiate the desired application class by using its namespace. I hope it make it a little clear :)

Great thank you, yes I get it.