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:
- As class name alias - simply declares short name for a class (must be declared outside of the class definition)
(manual: Using namespaces: Aliasing/Importing ) - To add a trait to a class (must be declared inside (at the top) of the class definition)
(manual: Traits) - 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
Difference Between a Language Construct and a "Built-In" Function in PHP
Why Are My Cookies Not Setting
How to Properly Use a Pdo Object For a Parameterized Select Query
Posting Raw Image Data as Multipart/Form-Data in Curl
Best Practice on PHP Singleton Classes
How to Loop Through a MySQL Result Set More Than Once Using the MySQL_* Functions
How to Check If an Array Contains a Specific Value in PHP
Format Bytes to Kilobytes, Megabytes, Gigabytes
Convert Plain Text Urls into HTML Hyperlinks in PHP
Dollar ($) Sign in Password String Treated as Variable
Remove Text Between Parentheses PHP
PHP Date() Format When Inserting into Datetime in MySQL
PHP Variables in Anonymous Functions
What's the Difference Between 'Isset()' and '!Empty()' in PHP