简体   繁体   中英

PHP: Use external class in a namespace

There is a class which handles the internationalization.

<?php

class Language{
    // ...
}

Now I created a renderer.php file which should handle all injections for HTML.

<?php
namespace Renderer;

include_once '../language.php';

function drawUserList(){
    // ...
    $group = Language::translate($groupName);
   // ...
}

In this file I need my Language class. But my compiler throws the following error message:

Undefined type 'Renderer\Language'.

The Language class is not part of a namespace. Why adds PHP a namespace to it? And why I am not able to use the class in my namespace function?

PHP Version 7.4.26

You have to use the keywork use :

namespace Renderer;
use Language; // indicate to use \Language

include_once '../language.php';

function drawUserList(){
    // ...
    $group = Language::translate($groupName);
   // ...
}

or use $group = \Language::translate() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM