Welcome Guest, Not a member yet? Register   Sign In
function that call other function inside the same controller
#1

[eluser]atoleon[/eluser]
Hello

Its possible create a method wich call other method in the same controller?
For example

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

class Galeria extends CI_Controller {

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

$this->load->model('general');
$this->load->database();
$this->load->helper ('url');
}

public function index () {
...
}

public function other() {

...
index ();
}
}

?>

this example show this error: Fatal error: Call to undefined function index()

Thank you everybody
#2

[eluser]TheFuzzy0ne[/eluser]
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Galeria extends CI_Controller {

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

        $this->load->model('general');
        $this->load->database();
        $this->load->helper ('url');
    }

    public function index ()
    {
        echo 'Hooray!';
    }

    public function other()
    {
        $this->index();
    }
}

You're not calling a function, you're calling a method, which means you need to access it view the object it resides in. i.e. $this->method_name(). Just like when you access a model or library method $this->model_name->method_name();

Also, it's recommended you omit the closing PHP tag to prevent problems with trailing whitespace.




Theme © iAndrew 2016 - Forum software by © MyBB