There is a class

class X { function __construct() { $this->array = array(); } function __destruct() { return $this->array; } function getAllData() { array_push($this->array, 'no_enter_data'); } } 

Call funkytsyu

 $result = new X; print_r($result->getAllData()); 

But returns an empty array why I can not understand.

  • return in the getAllData method getAllData not forgotten? - Suvitruf
  • And will not the destruk be launched after the GetAllData? - Vitali Malinovski

1 answer 1

If you want to see the result here - print_r($result->getAllData()); then you need to return the result:

 function getAllData() { array_push($this->array, 'no_enter_data'); return $this->array; } 

If you want to get at __destruct , then here is a little different.

The destructor is called, but the result of its work is not displayed, therefore return meaningless. It is necessary then so:

 class X { function __construct() { $this->array = array(); } function __destruct() { print_r($this->array); } function getAllData() { array_push($this->array, 'no_enter_data'); } } $result = new X; $result->getAllData(); 

PS It is worth considering that the destructor is called when the class is destroyed. Usually - at the end of the entire script.