M
M
matperez2020-05-26 01:24:02
Yii
matperez, 2020-05-26 01:24:02

Why does the Yii2 autoloader not see the class when accessing its constant?

A typical Yii2 Basic application . Just a fresh clone.

git clone [email protected]:yiisoft/yii2-app-basic.git
cd yii2-app-basic
composer install
./yii


The only change is to create an additional component and register it in the dependency container.

diff --git a/components/Something.php b/components/Something.php
new file mode 100644
index 0000000..18e0379
--- /dev/null
+++ b/components/Something.php
@@ -0,0 +1,10 @@
+namespace app\components;
+
+use yii\base\Component;
+
+class Something extends Component {
+    
+    const A = [];
+
+    public $b;
+}
\ No newline at end of file
diff --git a/config/console.php b/config/console.php
index 299656a..42e80d5 100644
--- a/config/console.php
+++ b/config/console.php
@@ -13,6 +13,14 @@ $config = [
         '@npm'   => '@vendor/npm-asset',
         '@tests' => '@app/tests',
     ],
+    'container' => [
+        'definitions' => [
+            \app\components\Something::class => [
+                'class' => \app\components\Something::class,
+                'b' => \app\components\Something::A
+            ],
+        ],
+    ],
     'components' => [
         'cache' => [
             'class' => 'yii\caching\FileCache',


As soon as I refer to a constant of this class, everything immediately falls, since the autoloader cannot find the specified class.
/vhosts/yii2-app-basic$ ./yii
PHP Fatal error:  Uncaught Error: Class 'app\components\Something' not found in /vhosts/yii2-app-basic/config/console.php:20
Stack trace:
#0 /vhosts/yii2-app-basic/yii(17): require()
#1 {main}
  thrown in /vhosts/yii2-app-basic/config/console.php on line 20


Without mentioning \app\components\Something::A everything works great.

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question