php_nub_qq
php_nub_qq

Reputation: 16025

The use of "use"

I was just playing around with my IDE and I noticed that when I use the "Fix code" option on a class it adds a bunch of lines on the top of the following type

use Someclass;
use \Ano\therClass;
use Iface;
...

I was wondering what exactly is the purpose of this, since the classes are going to be loaded on demand, is there a need to explicitly declare which classes are going to be used?

Upvotes: 0

Views: 117

Answers (2)

Waldz
Waldz

Reputation: 104

When U create a class, name it's package with keyword namespace

<?php
/**
 * CacheException.php
 */
namespace Doctrine\ORM\Cache;

class CacheException extends Exception {}

Elsewhere use can import only one class from package:

use Doctrine\ORM\Cache\CacheException;

throw new CacheException('Failed to cache');

Also use imports overal package with all it's classes:

use Doctrine\ORM\Cache;

throw new CacheException('Failed to cache');

More at http://php.net/manual/en/language.namespaces.importing.php

Upvotes: 0

user1797198
user1797198

Reputation:

Using use you can basically have different objects, functions, etc. with the same name, thanks to namespaces. When you write use in your code, you tell PHP to import items of a namespace and give it an alias.

Read more: http://php.net/manual/en/language.namespaces.importing.php

Upvotes: 1

Related Questions