131

I am having a little trouble with namespaces and the use statements.

I have three files: ShapeInterface.php, Shape.php and Circle.php.

I am trying to do this using relative paths so I have put this in all of the classes:

namespace Shape; 

In my circle class I have the following:

namespace Shape;
//use Shape;
//use ShapeInterface;

include 'Shape.php';
include 'ShapeInterface.php';    

class Circle extends Shape implements ShapeInterface{ ....

If I use the include statements I get no errors. If I try the use statements I get:

Fatal error: Class 'Shape\Shape' not found in /Users/shawn/Documents/work/sites/workspace/shape/Circle.php on line 8

Could someone please give me a little guidance on the issue?

Sunny Patel
  • 7,505
  • 2
  • 31
  • 42
Shawn Northrop
  • 5,320
  • 6
  • 37
  • 75
  • Also concerning this subject - http://stackoverflow.com/questions/33341955/does-the-import-or-use-order-affect-functionality-in-php – Peter Oct 26 '15 at 11:39

2 Answers2

186

The use operator is for giving aliases to names of classes, interfaces or other namespaces. Most use statements refer to a namespace or class that you'd like to shorten:

use My\Full\Namespace;

is equivalent to:

use My\Full\Namespace as Namespace;
// Namespace\Foo is now shorthand for My\Full\Namespace\Foo

If the use operator is used with a class or interface name, it has the following uses:

// after this, "new DifferentName();" would instantiate a My\Full\Classname
use My\Full\Classname as DifferentName;

// global class - making "new ArrayObject()" and "new \ArrayObject()" equivalent
use ArrayObject;

The use operator is not to be confused with autoloading. A class is autoloaded (negating the need for include) by registering an autoloader (e.g. with spl_autoload_register). You might want to read PSR-4 to see a suitable autoloader implementation.

Tomás
  • 3,310
  • 3
  • 20
  • 38
cmbuckley
  • 36,905
  • 8
  • 73
  • 90
  • So if i create another file called bootstrap.php and place an autoloader inside along with $circle = new Circle(); It includes the Circle.php but I am getting an error: Fatal error: Class 'Shape' not found in .../Circle.php on line 6. It appears to load Circle.php but not load Shape.php Circle is defined as: class Circle extends Shape implements ShapeInterface – Shawn Northrop May 11 '12 at 01:05
  • If I remove the namespace from the above classes the autoloader works fine. However when I have namespace in the interface of shape class I get the above error – Shawn Northrop May 11 '12 at 01:34
  • 1
    [Created a gist](https://gist.github.com/2658317) to give an example. Unfortunately gists can't have subfolders. Put the bootstrap.php in a folder and put the other classes in a subfolder named 'Shape'. – cmbuckley May 11 '12 at 08:21
15

If you need to order your code into namespaces, just use the keyword namespace:

file1.php

namespace foo\bar;

In file2.php

$obj = new \foo\bar\myObj();

You can also use use. If in file2 you put

use foo\bar as mypath;

you need to use mypath instead of bar anywhere in the file:

$obj  = new mypath\myObj();

Using use foo\bar; is equal to use foo\bar as bar;.

Charlie
  • 21,138
  • 10
  • 54
  • 85