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

[eluser]tomitzel[/eluser]
Hello,
I've build this function that gets 5 random products, but I get this error message:
Code:
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:
Code:
<?php

class Main extends Controller{
    
    function Main(){
        parent::Controller();
        $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
#2

[eluser]InsiteFX[/eluser]
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.

Enjoy
InsiteFX
#3

[eluser]tomitzel[/eluser]
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.
#4

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

Code:
'random_products' => _getFiveRandomProducts(5)

should be:

Code:
'random_products' => $this->_getFiveRandomProducts(5)
#5

[eluser]tomitzel[/eluser]
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