Jump to content

Include Files Class Scope


bpercival
 Share

Recommended Posts

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

Link to comment
Share on other sites

The problem is that you're using a function to include a file, which you should never do.

 

When you do that, the functions and classes are included on the global scope, but the variables remain in the function's scope, and are destroyed when the function completes.

Link to comment
Share on other sites

Examples:

 

file2.php

<?php

function helloWorld() {
return 'Hello World!';
}

$var = 'foobar';

?>

 

file.php

<?php 

function includeFile() {
include( 'temp2.php' );
echo 'In function - '.$var.'<br>';
}

includeFile();
// $var only exists in the function's scope
echo 'Outside of function - ';
echo $var.'<br>';

// functions included will exist in the global scope though
echo helloWorld().'<br>';

// will throw a fatal error, as helloWorld() already exists! use include_once or require_once instead
includeFile();

?>

 

output from file.php

In function - foobar
Outside of function - 
Notice: Undefined variable: var in /path/to/web/file.php on line 11

Hello World!

Fatal error: Cannot redeclare helloWorld() (previously declared in /path/to/web/file2.php:4) in /path/to/web/file2.php on line 5

Link to comment
Share on other sites

Thanks so much @XYPH. I finally figured that out after putting together the most simple page I could to simulate the issue and when I included the files directly and not through the function, it worked.

 

This is actually a word press theme that I'm modifying to include our header, footer etc and they basically do the call to a function to see if you've added stuff to the theme's header/footer in the word press web editor and not in a file. ie If (!stuff_added_in_online_editor) { include header.php }

 

I'll just have to train the admins of all the blogs that they can't modify the header or footer in the Wordpress.

 

Thanks again.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.