Home » Php » php – CodeIgniter HMVC object_to_array() error

php – CodeIgniter HMVC object_to_array() error

Posted by: admin April 23, 2020 Leave a comment


HMVC : https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/downloads

After downloading CI and copying over the HMVC, I’m getting the following error:

An uncaught Exception was encountered

Type: Error

Message: Call to undefined method MY_Loader::_ci_object_to_array()


Line Number: 300


File: /Users/k1ut2/Sites/nine.dev/application/controllers/Welcome.php
Line: 23 Function: view

File: /Users/k1ut2/Sites/nine.dev/index.php Line: 315 Function:

How to&Answers:

Just adding this here as the Link provided by Clasyk isn’t currently working…

The short version from that thread boils down to this…

In application/third_party/MX/Loader.php you can do the following…

Under public function view($view, $vars = array(), $return = FALSE) Look for… (Line 300)

return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));

Replace this with

if (method_exists($this, '_ci_object_to_array'))
        return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
} else {
        return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return));

It’s the result of a “little” undocumented change that the CI Devs implemented, which is fine!

There is a pull request on Wiredesignz awaiting action so he knows about it…

In the meantime, you can implement the above “diddle” and get back to coding 🙂


I got the solution.this is working for me.
On Line 300 of application/third_party/MX/Loader.php

This line generates an error with CI 3.1.3

return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));

Replace with this line.

return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return));


Found this Use this place in application / core / MY_Loader.php

From here https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/pull-requests/17/fix-loaderphp-for-ci-313/diff#comment-30560940

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the MX_Loader class */
require APPPATH."third_party/MX/Loader.php";

class MY_Loader extends MX_Loader
    /** Load a module view **/
    public function view($view, $vars = array(), $return = FALSE)
        list($path, $_view) = Modules::find($view, $this->_module, 'views/');

        if ($path != FALSE)
            $this->_ci_view_paths = array($path => TRUE) + $this->_ci_view_paths;
            $view = $_view;

        return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => ((method_exists($this,'_ci_object_to_array')) ? $this->_ci_object_to_array($vars) : $this->_ci_prepare_view_vars($vars)), '_ci_return' => $return));


HMVC doesn’t work with 3.1.3 (current version). But works with all versions up to 3.1.2. Just tested this myself from 3.0.0 upwards.


Add this lines in application/third_party/MX/Loader.php after line 307,

protected function _ci_object_to_array($object) 
    return is_object($object) ? get_object_vars($object) : $object;

However for 3.1.3 HMVC doesn’t work.

better luck.