There is a file with a class and the Profile.php function:

<?php class News { public static function getNewsList() { $api = "123123123"; //Функция большая, это её начало. 

There is a file which, in theory, includes the code above:

 <?php include_once ROOT. '/models/Profile.php'; class ProfileController { public function actionIndex() { $newsList = News::getNewsList(); echo $api; // Выдаёт ошибку:Notice: Undefined variable: api return true; } 

The function is connected, it is one hundred percent. But for some reason he does not see variables ... What is the problem?

    1 answer 1

    The scope of the $ api variable is the getNewsList () function. If you need to get a variable elsewhere, then you need to bring it into a separate method that will return it. In class News create method

     public static function getApi() { $api = '123123123'; return $api; } 

    And then call where you need

     $api = News::getApi(); 
    • Short, clear and effective. - Bim Bam
    • And how to get a lot of variables out of here? Api is pulled out perfectly, and what to do with others? Every time re-call? - Bim Bam