1

I have added PHP7 to my local server and I got this error:

Message: Array to string conversion Filename: libraries/Data_views_array.php Line Number: 59

data = $this->CI->$this_data[0]->$method($pass_var);

I found this post: PHP Notice: Array to string conversion only on PHP 7 and I changed my code to:

data = $this->CI->$this_data[0]->{$method($pass_var)};

but I got a different error:

Message: Call to undefined function get_page_id()

Everything works in PHP 5.6 and I don't know how to make it work in PHP 7.

Please help.

class Data_views_array {

    var $CI;

            public function __construct() {

                    $this->CI =& get_instance();

            }

            public function action_per_module($array_modules) {

               $array_to_display = array();
               $array_view_data = array();

                if (!$array_modules) {
                    return false;
                }
                else {
                    while (list($key, $this_data) = each($array_modules)) {

                        /*
                        *  $this_data[0] -  is the controller name
                        * loading this model
                        */

                       $this->CI->load->library($this_data[0]);

                          if ($this_data[2] == 'NULL') {
                               /*
                               * if there are no arguments (method and passing variable)
                               * the model is called
                               */
                               $data = $this->CI->$this_data[0];

                          }
                            else {
                              /*
                              * getting method and passing variable from arguments
                              */
                    //echo $this_data[2]. " - json<br>";
                              $obj = json_decode($this_data[2]);
                              $method = key(get_object_vars($obj));
                    //echo $method. " - method<br>";

                              $pass_var = $obj->$method;

                             /*
                              * getting data for view
                              */
                              $data = $this->CI->$this_data[0]->$method($pass_var);

                            }


                        /*
                        * name of the View
                        */
                        $view = $this_data[1];

                        /*
                        * adding the pair of View to the $array_view_data 
                        */
                        array_push($array_view_data, $view);
                        array_push($array_view_data, $data);
                        /*
                        * passing View Array to Display array
                        */
                        array_push($array_to_display, $array_view_data);
                        /*
                        * clear the array of the pair of View and passing variable
                        */
                        unset($array_view_data);
                        $array_view_data = array();

                    }
                }

                return $array_to_display;
            }

}

The first Class that is called - $this_data[0]== page_title_display from library has method: get_page_id($page_id). This class gets the Title of the page. Why the same script works in PHP 5.6 and it does not work in PHP 7? Why the get_page_id function is found in PHP 5.6 but not in PHP 7?

class Page_title_display {

    var $CI;

    public function __construct(){

            $this->CI =& get_instance();

   }

    public function get_page_id($page_id) {
        $this->CI->load->model('pageTitle_model');
        $title = $this->CI->pageTitle_model->getPagetitle($page_id);

        return $title;
    }
}
Serj
  • 41
  • 1
  • 9
  • what does `$this_data[0]` contain anyway? if its an object that does not have a function thats called `->get_page_id()` then it doesn't. try to debug per line to make sure – Kevin Dec 07 '17 at 03:28
  • The error is self explanatory. Whichever library you are calling doesn't have that function. Also what you are doing here looks really strange haha. – Alex Dec 07 '17 at 03:43
  • Alex, the page is composed from sets. Each set has its module. I keep the controller name(id) and View(id) for each set in DB. By reading it the page is loaded dynamically and if I need I can move each block (module) inside the page or even to add to different page, now in database but soon I will create a backend for it. All page information is stored in DB. I don't have anything coded except Controllers, Models and Views. – Serj Dec 07 '17 at 06:25
  • I would like to mention that this works in PHP 5.6 , I just wanted to update the PHP version to PHP7. This is the site: http://www.dyna3llc.com – Serj Dec 07 '17 at 06:37

1 Answers1

1

I solved this issue. For some reason PHP 7 does not like when $this_data[0] is used as a part of array.: data = $this->CI->$this_data[0]->$method($pass_var);

First solution: I assigned this value to a variable:

$libControl = $this_data[0]; data = $this->CI->$libControl->$method($pass_var);

Second solution: only $this_data[0] in brackets:

data = $this->CI->{$this_data[0]}->$method($pass_var);

Serj
  • 41
  • 1
  • 9
  • what is `$method`? depending on its value, `->$method($pass_var) ` and `->{$method($pass_var)}` are not equivalent statements – chiliNUT Dec 08 '17 at 01:55
  • If you take a look at Page_title_display class you will find the method get_page_id. $this_data[0] is the name of the class - in this case page_display_data, and it is a string (I mean the type of $this_data[0]). $method is the name of the method - in this case get_page_id and it is string. $pass_var is the page_id that is passed to the method. When I put all together: data = $this->CI->page_display_data->get_page_id(23); – Serj Dec 08 '17 at 02:27
  • Sorry I made the same mistake twice: the class is Page_titlt_display. – Serj Dec 08 '17 at 02:36