Using PHP Version 5.2.13
My question: How do I access a class and it's methods from an included file?
I have an Index.php page that calls two methods:
<?php get_header(); ?>
<?php get_footer(); ?>
and it creates a class in an include file
<?php include_once($_SERVER['DOCUMENT_ROOT'].'/includes/common.html');?>
$site = new WebSite($site_name); // Creates a bunch of properties, defines some methods, etc...
$site->initialize();
<?php get_header(); ?> ends up including a another file (header.php)
<?php get_header(); ?> ends up including a another file (footer.php)
All calls in Index.php to methods in my class work. ie <?php $site->display_section('column'); ?>
But calls in either the header.php or footer.php to methods in my class fail with "Call to a member function is not an object".
I understand what the error means but I don't understand why. I thought all functions and classes defined in an include file have global scope. As per PHP.NET's documentation:
"When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope."
The include that creates the class is something that I have inherited from another developer and I actually can't change the scope. I've tried with global $site = new WebSite($site_name);. It actually breaks the entire site.
How can I access classes, properties, methods in an include file?
Any help would be appreciated. I've been racking my head for several days now on it and it's probably some newbie thing that I am completely overlooking. I hope I've provided enough background and info.
Thanks,
Brian