use vs include as an import statement in PHP
source link: https://stackoverflow.com/questions/48263687/use-vs-include-as-an-import-statement-in-php/76356849
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
use vs include as an import statement in PHP
I have 2 simple files:
file1.php
<?php
namespace NM\data;
class MyClass
{
static function myStaticFunction(){
echo __NAMESPACE__." heheh";
}
}
function myfunction()
{
echo __FUNCTION__." some data";
}
const MYCONST = 1;
and file2.php:
<?php
// include 'file1.php';
use NM\data as NM;
class MyClass
{
}
function myfunction()
{
echo __NAMESPACE__." heheh";
}
const MYCONST = 2;
echo NM\myfunction();
The docs say that you can use use
as an importing command for namespaces. http://php.net/manual/en/language.namespaces.importing.php
I had little success with importing namespaces with just use
. For some other namespace to be used, I had to use include
. But if I use include
, than use
is performs just an alias.
Is is possible to import a namespace just with the use
keyword?
3 Answers
But if I use
include
, thanuse
is performs just an alias.
Yup, that's all use
does. use
just aliases namespaces. No more, no less. It is orthogonal to actually loading any piece of code from a file.
Actually there is a way to do so. Read about autoload functions in PHP, I would suggest to look on PSR-4 Autoloading
Before PHP 5.3 there was no namespace-s so files ware loaded by include function. On that time we had PSR-0 which loaded files by it's name but this was leading to long class names like "Mage_Admin_Model_Block". (this was resolved to file located in directory "Mage/Admin/Model/Block.php"
With namespaces introduced in php 5.3+ new autoloading standard was introduced, it is using namespaces to resolve path to file that contains definition of class that we want to use.
For example this
use Symfony\Bundle\FrameworkBundle\Controller\Controller
is resolved to: 'Symfony\Bundle\FrameworkBundle\Controller\Controller.php`
So PHP does not know where class file is located but with use of autoloader it can resolve where that file is present and load it if needed. You can write you're own code to do so but i would strongly suggest to stick to PSR-4 standard (PSR-0 is outdated now)
The include and import statements in PHP are used to include the content of another file into the current file. However, there are some key differences between the two statements.
- The
include
statement is used to include and evaluate the content of a file during the runtime of your PHP script. It allows you to insert the content of the specified file directly into the current PHP file at the location where the include statement is written. If the specified file cannot be found or included for some reason, PHP will generate a warning but continue executing the script.
Example:
<?php
include 'myfile.php';
// Rest of the code
?>
- In PHP, there is no direct
import
statement like in some other programming languages. Instead, PHP uses thenamespace
keyword for importing classes from other files or libraries. Thenamespace
keyword allows you better organization by grouping classes, and you can import classes from other files or libraries using theuse
statement.
Example
<?php
namespace MyNamespace;
use AnotherNamespace\SomeClass;
// Rest of the code
?>
- The
include
statement will only generate a warning if the file that is included cannot be found, whilerequire
function will generate a fatal error. This means thatinclude
statement is a more forgiving statement, and the script will continue to execute even if the included file is not found. - The
include
statement will include the entire contents of the included file, whileuse
statement will only include the classes, functions, and constants that are used in the current file. This means thatuse
statement is a more efficient statement, as it does not include any unnecessary code. - The
include
statement can be used to include any type of file, whileuse
statement can only be used to include PHP files. - In general,
include
statement is a good choice when you need to include a file that may not be available all the time, such as a configuration file.use
statement is a good choice when you need to include a file that is always available, such as a library of functions.
Here are some examples of how to use include
and use
:
// This will include the entire contents of the file `header.php`.
include 'header.php';
// This will only include the classes, functions, and constants that are used in the current file from the file `functions.php`.
use functions;
// This will include the file `footer.php` only if it exists.
if (file_exists('footer.php')) {
include 'footer.php';
}
Your Answer
Post as a guest
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK