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

Joe Bruno
35,909 PointsPHP (WordPress Plugin), extends, namespacing, and require
Hello All,
Before namespacing my plugin, I could extend a class without difficulty as the main plugin file (StripeTacular.php) required and included all the necessary plugin files; however, after namespacing the plugin (including the main file - every file, exactly the same way with "namespace StripeTacular;" placed on the first line), I now have to require any necessary files into every php file that extends or instantiates a class.
In other words, originally, the main plugin file ran all of the necessary includes and requires; however, after namespacing the plugin, I now must additionally run 'requires' within any additional file that extends or instantiates a class. E.g.
namespace StripeTacular;
require 'Customer.php';
require 'Store.php';
require 'Product.php'; // each of these are classes
class Cart extends Customer
{
What am I doing wrong? How can I simply autoload all of my classes with the namespace being in effect so that I do not have to specify the requires independently in each file and can keep them all neatly in the main file alone? Or is this simply the 'way it works.' This is my first attempt at namespacing using php's namespace declaration. Thanks in advance.
Joe
1 Answer

thomascawthorn
22,986 PointsHey!
Have you watched the most recent series by Phil Sturgeon? He shows you how to make your own autoloader.
Once you have an autoloader, you should be able to do:
<?php namespace StripeTacular;
use StripeTacular\Customer;
use StripeTacular\Store;
use StripeTacular\Product;
class Foo extends Customer {
new Customer;
}
This would also give the same result:
<?php namespace StripeTacular;
class Foo extends StripeTacular\Customer {
new Customer;
}
but it's not very nice!
Those classes will also require a namespace of 'StripeTacualr'
Right now you're just doing exactly the same as an autoloader would, but using an autoloader would be a lot smoother.
Because all of your classes are in the same directory (by the look of things), you may even be able to omit StripeTacular from your 'use' declaration.