How Does the Keyword "Use" Work in PHP and How to Import Classes With It

How does the keyword use work in PHP and can I import classes with it?

use doesn't include anything. It just imports the specified namespace (or class) to the current scope

If you want the classes to be autoloaded - read about autoloading

How and where should I use the keyword use in php

In PHP, the keyword use is used in 3 cases:

  1. As class name alias - simply declares short name for a class (must be declared outside of the class definition)
    (manual: Using namespaces: Aliasing/Importing )
  2. To add a trait to a class (must be declared inside (at the top) of the class definition)
    (manual: Traits)
  3. In anonymous function definition to pass variables inside the function
    (manual: Anonymous functions)

PHP import classes with use keywords

As per comment from @deceze, you will either need to explicitly import the additional class, using a require statement, or autoload.

The use statement is for aliasing a class, and as @deceze said, can be used to pull in a class from a different namespace, or to avoid a class conflict.

Having a class called 'Main' may not be ideal. Is it a singleton, or will there be multiple 'Main's?
Maybe this class would be better named 'App'.

In the longer term you will want to learn about using namespaces, so that if you use other people's classes and plugins, you won't have conflict. I've added a solution, and also some broader info below.

To get you off the hook:

Classes/MainClass.php

<?php

require_once 'Classes/AdminFrontEnd.php';

class Main { /* etc... */



Further reading I would recommend:

Examples for creating an autoload:

http://php.net/manual/en/language.oop5.autoload.php

You'll probably want to learn about namespaces too:

http://php.net/manual/en/language.namespaces.php

I also highly recommend reading about interoperability coding standards. It's a lot to take in to begin with, but it will help you understand the logic behind using namespaces, and autoloaders.

http://www.php-fig.org/

how does keyword 'use ' works in php (laravel)

in Laravel app there is a public folder . there is a file name index.php..which require..

require __DIR__.'/../vendor/autoload.php';

Note index.php is the entry point of Laravel application

in vendor folder.. if you go there there is a file autoload.php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInita990d9507b876ea05d2300077581f677::getLoader();

autoload.php file autoload all classes file...

Import class conditionally with the keyword 'use'

The only thing use does is to alias a class name. That's it. Nothing more.

Instead of having to repeatedly write the fully qualified classname in your script:

$q = new \Foo\Bar\Baz\Quux;
if ($q instanceof \Foo\Bar\Baz\Quux) ...

You can shorten that to:

use Foo\Bar\Baz\Quux;

$q = new Quux;
if ($q instanceof Quux) ...

As such, it makes absolutely no sense to want to use use conditionally. It's just a syntactic helper; if it could be used conditionally your script syntax would become ambiguous, which is something nobody wants.

It doesn't reduce code loading, because code is only loaded explicitly by require/include calls or via autoloading. The latter one is greatly preferred, since it already lazily springs into action only when needed.



Related Topics



Leave a reply



Submit