Results 1 to 4 of 4

Thread: Function definition oddity, same script or included files.

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default Function definition oddity, same script or included files.

    If a function is defined in the same file, but after the call to that function, there is no problem.

    However if a function is defined in a different file and this file is included after the function call, a fatal error occurs. Fatal error: Call to undefined function func() in..

    This is a more academic question than anything, because the best practice to to always define the function prior to calling it.

    PHP Code:
    <?php
    // THIS DOESN'T WORK

    echo func();

    require(
    'func.php'); // includes the func() definition

    exit;
    PHP Code:
    <?php
    // THIS DOES

    echo func();

    function 
    func() {

    return 
    'test';

    }

    exit;

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    PHP does the two-pass parse model, to check for functions and load them before actually executing the script. Anything except a basic function definition (includes, conditional definitions, &c.) breaks this, and such functions cannot be preloaded.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Thanks Twey,

    Would you explain the two-pass parse model for me? Whenever you have a minute.

    Thanks a lot. Glad you are active here.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Quote Originally Posted by myself
    check for functions and load them before actually executing the script
    There's not much else to say about it, really
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •