Home  >  Article  >  Backend Development  >  Use keyword in various aspects of life in PHP

Use keyword in various aspects of life in PHP

autoload
autoloadOriginal
2021-04-02 14:15:422016browse

Use keyword in various aspects of life in PHP

The use keyword is not only involved in the oriented process of PHP, It also steals the show in PHP object-oriented. In the actual development process, it is also spread throughout the source code. This article will make a summary of the use keyword.

1. Reference for namespace

<?php
namespace admin\controller;
use \core\controller; //引入命名空间
class ArticleController extends Controller{
    public function index(){

  }
}
?>

2. Keyword for alias

namespace space;
function display(){}
class Man{}
const PI = 3.14;
 
namespace space1;
class Man{}
//引入空间元素
//use space\Man;                //错误:当前空间已经存在Man
use space\Man as M;
use function space\display as dis;
use const space\PI as D;

3. Used for the introduction of trait feature capabilities

<?php
trait A{
    function testTrait(){
        echo &#39;This is Trait A!&#39;;
    }
}

class B {
    use A;
}

$b = new B();
$b->testTrait();

?>

4. In anonymous functions Reference local variables in

<?php 
function F1(){
    $ok="HelloWorld";
    $a=function() use($ok) {
        echo "$ok";
    };
    $a();
}
F1();
?>

Recommended: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of Use keyword in various aspects of life in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn