I
I
Igor Vasiliev2018-02-25 19:49:30
PHP
Igor Vasiliev, 2018-02-25 19:49:30

How to register a class in PHP?

Hello.
I have
not encountered this problem before, but now I have. I wrote a class in php, everything is ok.
I registered the namespace through the IDE - I checked it, it is specified correctly, the bonds see the class, everything is connected correctly:

<?php
use test\Test;
...
?>

The file Test.phpis in the "test"
folder. Accordingly, the class itself:
<?php
namespace test;

class Test 
{
...
}
?>

Actually, the error:
Fatal error: Class 'test\Test' not found
I read that classes need to be registered somehow, the site itself is not on the engine, and on OpenServer. Where is the error, what did I do wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maksim Fedorov, 2018-02-25
@Maksclub

Use Composer and include the required class via autoload
The word usedoes not include the class file, it is needed to limit the namespace

U
Uwe_Boll, 2018-02-25
@Uwe_Boll

is there an autoloader?
if not then read

A
Antonio Solo, 2018-02-25
@solotony

because use imports names and doesn't load files
, that's how it will work

<?php

require_once 'test\Test.php';
use test\Test;

$a = new Test;

var_dump($a);

?>

or without use
<?php
require_once 'test\Test.php';

$b =  new test\Test;

var_dump($b);
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question