Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use vs include as an import statement in PHP

Tags:

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?

like image 761
sanjihan Avatar asked Jan 15 '18 13:01

sanjihan


People also ask

What does the INCLUDE statement do in PHP?

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML

What is the difference between require and include in PHP?

PHP include vs. require. The require statement is also used to include a file into the PHP code. However, there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute:

How do I include a PHP file in another PHP file?

PHP include and require Statements It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script

What is the purpose of the INCLUDE statement in a website?

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.


2 Answers

But if I use include, than use 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.

like image 184
deceze Avatar answered Sep 18 '22 19:09

deceze


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)

like image 26
Maxvt Avatar answered Sep 19 '22 19:09

Maxvt