Namespace in PHP Does not work

Asked

Viewed 319 times

0

Hello, I’m trying to use the namespace and the use but is resulting in Not found

Error: Fatal error: Uncaught Error: Class 'app\lib\Teste' not found in C:\xampp\htdocs\index.php:5 Stack trace: #0 {main} thrown in C:\xampp\htdocs\index.php on line 5

index php.:

<?php

require_once './vendor/autoload.php';
use \app\lib\Teste;

new Teste();

?>

htdocs/app/lib/Teste.php

<?php

namespace app\lib;


class Teste {


    public function __construct() {
        echo "Hello world";
    }

}


?>

Composer.json

{
    "require": {
        "php": ">=5.3.3"
    },
    "require-dev": {
        "phpunit/phpunit": "^4.3"
    }
}

  • So you can use this: new Teste();, first do this: use \app\lib\Teste AS Teste;, this is the first problem, see if it works.

  • Hello, thank you for the answer, I’ve tried this and still nothing.

  • Post the Composer.json as well

  • 1

    {&#xA; "require": {&#xA; "php": ">=5.3.3"&#xA; },&#xA; "require-dev": {&#xA; "phpunit/phpunit": "^4.3"&#xA; }&#xA;}&#xA;

  • 1

    See if you can get help https://answall.com/questions/40051/como-usar-namespace-numa-classe/40511#40511

  • Opa thanks for the reply, still nothing , I tried to do this: new app\lib\Teste(); and it hasn’t yet, I don’t understand why it’s not found because there is this directory and this class.

  • try calling app lib only

  • If I call only the use app\lib; and after that I call the new Teste(); return this: Fatal error: Uncaught Error: Class 'Teste' not found in C:\xampp\htdocs\index.php:13 Stack trace: #0 {main} thrown in C:\xampp\htdocs\index.php on line 13 , i think it must be some . php extension problem in use or something like.

  • You need to set autoload on your Composer.json first

  • If you are using Composer you should run Composer dump-autoload after creating the classes for the mapping to be updated.

Show 5 more comments

1 answer

0

I did some simulations and it worked, maybe the problem was at the time of instantiation, test something like this and show what returned:

use app\lib\Teste as T;

    public $teste;

    $this->teste = new T();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.