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

Use keyword in various aspects of life in PHP

autoload
Release: 2023-04-09 20:46:02
Original
2032 people have browsed it

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(){

  }
}
?>
Copy after login

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;
Copy after login

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();

?>
Copy after login

4. In anonymous functions Reference local variables in

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template