I
I
Ivan Ivanov2019-06-25 19:33:57
PHP
Ivan Ivanov, 2019-06-25 19:33:57

Why doesn't a method work after being called in another method?

Hello!
Property in the Controller class There are two methods for sending data to a view in the Controller class:
public static $vars = [];

public static function set($vars)
{
    self::$vars = $vars;
}

and
public static function setMeta($description)
{
        $generator = 'Название сайта';
        self::set(compact('generator', 'description'));
}

It is passed to the view like this:
new View(Controller::$vars);
If called in some other controller like this:
public static function indexAction()
{
    Controller::setMeta('Описание страницы'); // работает, во вьюхе 2 переменных: $generator и $description
    $test = 'Значение'; // для примера
    Controller::set(compact('test')); // не работает, во вьюхе нет переменной $test
}

If the methods are interchanged in a call, then only one is executed - the first one from the top :)
What did I screw up here?
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Ivanov, 2019-06-26
@Artsiom_Ryzhanki

DECISION

class Controller
{
    public static $vars = [];
    public static $meta = [];

    public static function set($vars)
    {
        self::$vars = $vars;
    }

    public static function setMeta($description)
    {
        self::$meta['generator'] = 'Название сайта';
        self::$meta['description'] = $description;
    }
}

class MainController
{
    public static function indexAction()
    {
        Controller::setMeta('Описание страницы');
        $meta = Controller::$meta;
        $test = 'test'; // ещё какие-то данные
        Controller::set(compact('meta', 'test')); // во вьюху прилетают $meta['generator'], $meta['description'], $test
    }
}

D
Dmitry Derepko, 2019-06-26
@xEpozZ

And where is the most important part called new View(Controller::$vars);?
And what order of execution of methods. From and to.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question