PHP classes and include()

Discussion in 'OT Technology' started by Seeders, Jul 18, 2008.

  1. Seeders

    Seeders OT Supporter

    Joined:
    Jul 10, 2007
    Messages:
    3,750
    Likes Received:
    0
    I have a php class, with a function called "getContent". In the function "getContent()" i need to use a bunch of other functions to process and output the content. These other functions are all written in a separate file called "funcs.php" which is referenced by other php programs that use them. How and where do I call "include('funcs.php');' so that these functions can be seen within my getContent() function?

    heres my class definition:

    Code:
    <?php
    class block_classlinks extends block_base {
            function get_content() { 
                  include('../funcs.php');
                  include('../open_connection.php');                
                  $perm = $USER->idnumber;                 
                  $courses = getCourses($perm,$conn);
    
    funcs.php looks like this:

    Code:
    <?
            function getCourses($perm,$conn){
                 blah blah
                 return $result;
            }
    ...
    ..
    ?>
    
    this throws the error:
    Fatal error: Call to undefined function: getcourses() in
    /path/block_classlinks.php
    on line 22
     
    Last edited: Jul 18, 2008
  2. Seeders

    Seeders OT Supporter

    Joined:
    Jul 10, 2007
    Messages:
    3,750
    Likes Received:
    0
  3. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    WHY are you trying to include an external file with just functions? they should be members of a class, and then you would "require_once" the class and create the object, etc.
     
  4. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    just put it at the top of the class file: require_once('../funcs.php')

    i imagine it's just some extra functions (akin to the standard built-in php functions) that are commonly used, but not really related to the class. if they are related to the class they should be member functions.
     
  5. Seeders

    Seeders OT Supporter

    Joined:
    Jul 10, 2007
    Messages:
    3,750
    Likes Received:
    0
  6. Seeders

    Seeders OT Supporter

    Joined:
    Jul 10, 2007
    Messages:
    3,750
    Likes Received:
    0
    putting it at the top breaks it because the class has to be defined in a very specific way (its a block in Moodle). otherwise i think you'd be right about that.
     
  7. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    You know, the error message you posted (assuming you copied/pasted the error) shows you calling getcourses() (note the lowercase). The function definition is getCourses() (note the uppercase). Have you double-checked this?
     
  8. Seeders

    Seeders OT Supporter

    Joined:
    Jul 10, 2007
    Messages:
    3,750
    Likes Received:
    0
    ya i found that strange also. i triple checked it...but still dont know why the error would change the capitalization..
     

Share This Page