O
O
Onatskyi2019-06-16 18:43:42
PHP
Onatskyi, 2019-06-16 18:43:42

How to use a function as a class (object)?

Hello.
How to use a function as a class?
I mean:
$locale = app()->getLocale();
How is it implemented and where can I find information on this issue? Thank you in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lazy @BojackHorseman PHP, 2019-06-16
@Onatskyi

the function simply returns an instance of the class, not a class. what is unclear here? there were PHP versions where such a call was generally prohibited.

E
Evgeny Romashkan, 2019-06-16
@EvgeniiR

class App {
    public function getLocale();
}

function app {
    return new App(); // в вашем примере тут синглтон
}

Use the transition to the definition in your IDE, or install PhpStorm if your development environment does not have such an opportunity to track where this function is declared
ps but in general, the approach is very so-so, there is both static and global state. I saw the global App in php only in the Yii framework, and they changed their minds and seem to cut it out in version 3 (if there is one, of course)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question