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

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

There is a file that includes the code above:

 <?php include_once ROOT. '/models/Profile.php'; class ProfileController { public function actionIndex() { $api = News::getNewsList(); echo $api; // Выводит на экран APi //Когда я ещё раз вызываю что-то: $logmein = News::getNewsList(); echo $logmein //Notice: Constant DB already defined in return true; } 

The function is connected, api is even displayed on the screen, but how can I derive other variables from this file? If you re-call a function, you crash a lot of errors, saying that what I am calling is already in use. How correctly can the other variables be derived?

  • show how you re-call, and what error occurs - Bookin
  • @Bookin Made the Editing - Bim Bam
  • 2
    1 - return ends the function. getNewsList returns only $api . You can return an array: return array("api" => $api, "logmein" => $logmein); , and in actionIndex to get access to values ​​by indexes. 2 - Constant DB already defined in what else is a DB? Probably a repeated call to the getNewsList method leads to a redefinition of the constant. - UserName
  • @UserName Made return array (...) But when I tried to output via var_dump ($ user_data ['logmein']) ;, he writes that the variable is not defined and the value of the array is null - Bim Bam
  • Probably the operation that results in a value being set is working incorrectly, so logmein set to null. It is necessary to localize the problem, that is, to check the results of calls to functions that set the values. - UserName

1 answer 1

In your version in the profile.php file, the getNewsList () function will always return $ api.

To return different values, you need to create separate functions for each value and call them from actionIndex() , for example:

 public static function getNewsListApi() { $api = "123123123"; return $api; } public static function getNewsListLogMeIn() { $logmein = "xyz"; return $logmein; } 

Calling functions:

  $api = News::getNewsList(); $logmein = News::getNewsList(); 

Or, the second option, return the values ​​of the array:

 public static function getNewsList() { $api = "123123123"; $logmein = "xyz"; $arr = array( "api" => $api, "logmein" => $logmein ); return $arr; } 

And process as follows:

  $data = News::getNewsList(); $api = $data['api']; $logmein = $data['logmein']; 
  • Is it possible to just call News :: getNewsList (); without giving her anything? I did it and everything works. Did I do bad? Or is it possible? - Bim Bam
  • I do not quite understand the logic of the program further, not seeing the rest of the code, so I find it difficult to answer. - Neodev
  • If you do not need to receive anything from the function and all the logic is driven out of it, you can simply call News :: getNewsList (); without assignment. If I correctly understood what assignment you are asking about) - Neodev