E
E
Evgeny Musonov2020-01-16 07:47:37
Yii
Evgeny Musonov, 2020-01-16 07:47:37

How to include nested classes when running via PHP CLI?

I call the script from the console
php /var/www/myFile.php
In it, I connect the class using this code (found on the internet)

function classes($class)
{
    $app = dirname(__DIR__);

    $class = str_replace('app\\', '', $class);

    $file = $app . "/" . str_replace("\\", "/", $class) . ".php";

    if (file_exists($file)) {
        require_once $file;
    }
}

spl_autoload_register('classes');

But if the plug-in class contains other classes, then it does not see the nested ones.
This is how it works
function classes($class)
{
    $app = dirname(__DIR__);

    $class = str_replace('app\\', '', $class);

    $file = $app . "/" . str_replace("\\", "/", $class) . ".php";

    if (file_exists($file)) {
        require_once $file;
    }
}

spl_autoload_register('classes');

// /var/www/www-root/data/www/dev.goti.ru/app/components/Xml.php

$class = new Class();

But if other classes are called in Class.php, then they are not included. I do all this in yii2.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2020-01-16
@evmusonov

You have Yii in your tags. Why didn't you create a command (console controller) using the framework itself and run it through php yii <controller>/<action>?
upd: Here everything is described quite well and clearly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question