views:

102

answers:

1

Hi,

I'm getting a "Call to a member function process_data() on a non-object in page.class.php on line 35" even though the object has been called.

Here is the index.php extraction showing the object being instantised

// require our common files
require("modules/module.php");
require("registry/objects/datetime.class.php");
require("registry/objects/page.class.php");


// load in all the objects
$datetime = new dateandtime;
$page = new page;
$module = new module;

It then passes to the Process class

        require("template.class.php");
     $template = new template($php_path . "controllers/themes/adm/" . $page . ".html");

     // Place in both commonly used language and page specific language
     $template->language($php_path . "controllers/language/en/adm/common.php");
     $template->language($php_path . "controllers/language/en/adm/" . $page . ".php");

     // Tell the page's module to load in data it needs
     $module->process_data("module_" . $page);

     // Output the final result
     $template->output();

It's at this point PHP is throwing the error. The contents of the module.php file is as follows

class module {

    public function process_data ($child) {
     require($child . ".php");
     read_data();
     return true;
    }
}

I've tried moving the instance declaration to within the second pasted code, but that generates more errors, because the class that "module" calls in then uses some of the "template" classes as well - so the same issue occurs just further down the line.

What am I getting wrong her, or completely missing, I'm sure it's the latter but I really need help here. Thanks

A: 

It looks to me as if variable $module was not in the scope when you try to call object method. Could you try var_dump($module) before $module->process_data("module_" . $page). What is the result of this function? Quick solution may be declaring $module global, but globals are not a very good idea anyway (but you may check if it works).

empi
var_dump indeed returned a null. I'd tried global before and this didn't resolve the issue.I've read the documentation around objects and classes, but it's not enlightened me; certainly no one with problems across multiple files/classes.
jakeisonline
but if you declare variable $module global in the first file, and the you write global $module; var_dump($module); what do you get? still null?
empi
Bingo! Didn't need the global declaration in the first file, but adding global $module; var_dump($module); just before the use of the object seems to have worked just fine. What's the explanation there. As now this has given the same error for a different object, in the next file being called.
jakeisonline
problem is in variable scope not in class declaration or anything else. when you declare a variable in a file (outside any function) its scope is limited to that single file. if you want to use it in other file you have to declare it global.
empi
would it be better practise to declare that global variable from within the class itself (before any functions) or just keep it to before the object is called for use?
jakeisonline
I strongly recommend not using globals. There are many good patterns to avoid them. Short article http://blog.case.edu/gps10/2006/07/22/why_global_variables_in_php_is_bad_programming_practice, but many more can be found googling ;)
empi
the reason i wanted you to try this solution with global was just a quick fix to your problem, better solution can be found by examining your classes and deciding if you want to create a singleton or maybe some static functions, etc.
empi
Yeah, did try googling but I think I must have been searching for the wrong thing, StackOverflow was my final destination. Thanks very much for your help!
jakeisonline