132

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?

1

2 Answers 2

191

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
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
Created a gist 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'.
17

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;.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.