1

So, I have a class and from within a function in the class I am trying to call a global function, but I get the error message:

Fatal error: Call to undefined function ()

  • the line number the above function refers to is the line in which:

    return get_session_user_id(session_id());

occurs on in the below class function

Class Ref {
    function treat_var( $var , $table ) { global $link;
    // do stuff
    return get_session_user_id(session_id());
    }
}

The treat_var function is a method in a class and is called by means of:

$ref = new Ref;
$ref->treat_var($var,$table);

The get_session_user_id function is a global function defined in an included in a file of functions and works just fine outside of the class and its code consists of:

// this function calls a USER_ID *IF one exists* from a session by using the session ID
function get_session_user_id($sessionID) { global $link;
        $result = mysqli_query($link, "SELECT USER_ID AS ID from AAB_SESSIONS WHERE PHPSESSID = '$sessionID'");
        $r = mysqli_fetch_object($result);
        return $r->ID;
}

How do I call that function from within the class?

6
  • 1
    Please include more code. This part seems to be fine. What line is the code in? Commented Nov 14, 2015 at 11:41
  • All the other code functions properly, if I return a string instead of the value derived from the function it returns the string and everything is happy. Still need more code? Commented Nov 14, 2015 at 11:45
  • 1
    We need more detail. Commented Nov 14, 2015 at 11:47
  • Jeff, show which line returns the error. Commented Nov 14, 2015 at 11:51
  • I have just expirenced the very same problem with following code inside class function: $emailAddresses = array_column($recipients, 'email'); Error is: Call to undefined function MyPackage\array_column() Commented Sep 7, 2016 at 9:20

2 Answers 2

2

Assure that this global file where function get_session_user_id() is defined is loaded before class Ref is loaded.

In other words class loading should go in this way:
1. Include global file which contains impl of get_session_user_id()
2. Load class Ref

To check what is going on add this inside the Ref.php before the class definition

if (!function_exists('get_session_user_id')) {
    die('GLOBAL FILE NOT LOADED and get_session_user_id() function not available!!!');
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Gandra, I like this because rather than having to 'require_once' in several files, I address the root cause of the issue which is the fact that the function is not defined until after the class definition.
0

If you trying to call get_session_user_id() and this function is undefined, check, what file with this function is undefined, try to write

require_once "...filename with get_session_user_id() function...";

at begin of file with your class Ref.

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.