Vex Star

Vex Star

Computers and Programming

Vex Star RSS Feed
 
 
 
 

PHP classes and include()

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:

<?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:

<?
        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
i guess this kind of thing is not allowed because class definitions cant be defined at runtime.

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.
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.
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.
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?

ya i found that strange also. i triple checked it…but still dont know why the error would change the capitalization..

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Leave a Reply