Welcome Guest, Not a member yet? Register   Sign In
Call to undefined function

I've build this function that gets 5 random products, but I get this error message:
Fatal error: Call to undefined function _getfiverandomproducts() in /home1/nbxhostc/public_html/tomitzel/application/controllers/main.php  on line 24
But here is my controller with the function defined:

class Main extends Controller{
    function Main(){
        $categories = $this->category_model->GetCategories();
        $this->template->set('categories_list', $categories);
    function _getFiveRandomProducts($count){
        $products = $this->product_model->GetProducts();
        $random_products = array();
        $counter = 0;
        for($i = 0; $i < $count; $i++){
            $random_products[$counter++] = $products[rand(0, count($products))];
        return $random_products;
    function index(){        
        $data = array(
        'title' => 'Colon Free Zone',
        'random_products' => _getFiveRandomProducts(5)
        $this->template->load('template', 'homepage', $data);
Any suggestion what is wrong here, I think i'm missing here something very obvious Smile

CodeIgniter uses the underscore char to mark a
function/method as private which means you cannot
access it, take the underscore char of the function.


I try to acces the function index(), not _getFiveRandomProducts from outside. I want the function to be private and if its private it should work to call it from index.

Anyway, I tried to remove the underscore, the same problem, I don't understand why the error message refers to functions name with all charaters low case and not as I wrote the function.

The problem isn't the underscore, the underscore itself has no effect on the visibility of a method/member. Like tokyo said:

'random_products' => _getFiveRandomProducts(5)

should be:

'random_products' => $this->_getFiveRandomProducts(5)

Thank you tokyo and nectrum, this was such a stupid mistake from my part Smile
Was too tired to notice it.

Theme © iAndrew 2016 - Forum software by © MyBB