M
M
Maybe_V2016-06-11 20:36:24
Yii
Maybe_V, 2016-06-11 20:36:24

How to properly use your own class?

I use Yii2 advancedto unload a bit of code in the Controller.
I want to create a third party class and use it. I made a folder in commonand wrote a class in it, indicated namespace, and connected it ( use) in the controller. But it gives an error - this class does not exist!
How to properly implement such a task?
Where to create? And how to connect the class correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-06-12
@webinar

You can create it anywhere, the main thing is that the namespace takes into account "anywhere")))
for example, we create common/components/MyComponent.php
in it:

<?php
namespace common\components;

class MyComponent
{

    public static function fuckMe($name)
    {
        return 'I fuck, '.$name;
    }
}

in the controller at the top add
in the controller action
echo MyComponent::fuckMe('Вася');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question