michaelT
michaelT

Reputation: 1711

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

Upvotes: 1

Views: 316

Answers (1)

user17871170
user17871170

Reputation:

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

Upvotes: 2

Related Questions