0

I am starting out with codeigniter, but the documentation is horribly written and doesn't actually work with the new version. My issue is that I cannot call a function within a model.

Here is my model: User.php

 <?php

    class User extends CI_Model {

        function __construct()
        {
            parent::__construct();
        }
    }
    function test($x)
    {
        return $x;
    }

    ?>

And my controller: welcome.php

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

class Welcome extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->model('User');
        echo $this->User->test('darn');
        $this->load->view('welcome_message');
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

?>
3
  • Your file should be named user.php (lower-case). Commented Sep 11, 2014 at 15:58
  • How does it "not work"? Do you see an error message? The documentation is fine, and everything should work if you follow it. Commented Sep 11, 2014 at 15:59
  • I changed the name to user.php and I get the error PHP Fatal error: Call to undefined method User::test() in /var/www/html/application/controllers/welcome.php on line 28 Commented Sep 11, 2014 at 16:00

1 Answer 1

1

Check your {}s, your test function is outside your User class. Move it inside the class, then $this->User->test() will work.

<?php
    class User extends CI_Model {

        function __construct()
        {
            parent::__construct();
        }

        function test($x)
        {
            return $x;
        }
    }
?>

There's nothing "horrible" about the documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.