B
B
Bauyrzhan (Baur) Sovetkali2018-03-16 13:49:23
PHP
Bauyrzhan (Baur) Sovetkali, 2018-03-16 13:49:23

Why is the class not visible when using a namespace alias?

Hello.
File structure:
- App
-- ctrls
--- MyClass.php
- index.php
MyClass.php code:

<?php
namespace App\ctrls;

class MyClass{
    public $str = 'test';
    public static function getMe()
    {
        echo 'My namespace App. My name is getMe()';
    }
}

?>

Index.php code ( running ):
<?php
require "App/ctrls/MyClass.php";

App\ctrls\MyClass::getMe();
?>

But this is not working:
<?php
use App\ctrls as path;

$o = new path\MyClass.php;
?>

Error: Fatal error: Uncaught Error: Class 'App\ctrls\MyClass' not found in...
Please tell me why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nick Sdk, 2018-03-16
@Sovetkali

read about autoloading
https://github.com/codedokode/pasta/blob/master/ph...
in particular, about PSR-4
install the composer, if it's not worth
in the root of your project, create a file composer.json with content

{
    "autoload": {
        "psr-4": {
            "App\\": "App"
        }
    }
}

and run through the console (terminal) at the root of the project
composer install

D
dmitriy, 2018-03-16
@dmitriylanets

where is require "App/ctrls/MyClass.php";?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question