Search code examples
phpkohanakohana-3

Error : Undefined variable when passing parameter to method in Kohana Controller?


public function get_entity_keyNumber($entity)
    {
        $this->session = Session::instance();   
        (int)$lastNumber = 0;
        $user_data= array_keys($this->session->as_array());
        $user_experience = array_filter($user_data,function($value){return strstr($value, $entity);});
        if(!empty($user_experience))
        {
            $lastElement = end($user_experience);
            (int)$lastNumber = substr($lastElement,-1);
            $lastNumber++;
        }
        return $lastNumber;
    }

this function is doing that it return me last character of last key in $user_data and Iam incrementing to it by casting it to Integer.

$user_data = array("experience0") like if i use this function:

 (int)$lastNumber = get_entity_keyNumber("experience");

It will return me 1 and i will add another array to a session with name experience1 then experience2,experince3 so it cannot overwirte the keys in session

when iam using this function why is this throwing error: Call to undefined function get_entity_keyNumber() or sometimes $entity variable undefined Iam new to php and as well as to kohana

when using inline it works perfectly.


Solution

  • Try this:

    $instance->get_entity_keyNumber("experience");
    

    $instance means an instance of the class that the method get_entity_keyNumber belongs to.

    In the class definition field, it'll be $this.

    I'm sorry for not knowing about kohana.