By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,889 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,889 IT Pros & Developers. It's quick & easy.

Including file Inside class

P: n/a
I'm trying to setup a class that will allow a file to be included on
the page. It goes something like:

<?php
class myClass {
... ... ...
function includeFile(){
if(file_exists($this->include_file)){
include($this->include_file);
} else {
$this->error("file missing");
}
}
... ... ...
}

$foo = "bar";

$page = new myClass();
$page->include_file = "the_file.php";
$page->includeFile();
?>

// the_file.php
<?php echo $foo; ?>

It has something to do with variable scope and accessing globals or
something, I'm not sure. My question is how should I setup my class so
that I do not have to use "return" at the bottom of any files I want
to include with my class? I want the includes to function just like
normal includes.

I tried

<?php
class myClass {
... ... ...
function includeFile(){
global $GLOBALS;
global $HTTP_SERVER_VARS;
if(file_exists($this->include_file)){
include($this->include_file);
} else {
$this->error("file missing");
}
}
... ... ...
}
but that still doesn't make $foo print "bar".

Thanks for any help on how to accomplish this.
Sep 8 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Right now, i have found this solution by adding
extract($GLOBALS);
to the top of my function.

I would now like to somehow get the variables inside the include file
back into the global variable scope, making this function work just
like a normal include file. Is that possible?

On Sep 8, 11:35*am, briandichiara <briandichi...@gmail.comwrote:
I'm trying to setup a class that will allow a file to be included on
the page. It goes something like:

<?php
class myClass {
* * *... ... ...
* * *function includeFile(){
* * * * * if(file_exists($this->include_file)){
* * * * * * * *include($this->include_file);
* * * * * } else {
* * * * * * * *$this->error("file missing");
* * * * * }
* * *}
* * *... ... ...

}

$foo = "bar";

$page = new myClass();
$page->include_file = "the_file.php";
$page->includeFile();
?>

// the_file.php
<?php *echo $foo; *?>

It has something to do with variable scope and accessing globals or
something, I'm not sure. My question is how should I setup my class so
that I do not have to use "return" at the bottom of any files I want
to include with my class? I want the includes to function just like
normal includes.

I tried

<?php
class myClass {
* * *... ... ...
* * *function includeFile(){
* * * * * global $GLOBALS;
* * * * * global $HTTP_SERVER_VARS;
* * * * * if(file_exists($this->include_file)){
* * * * * * * *include($this->include_file);
* * * * * } else {
* * * * * * * *$this->error("file missing");
* * * * * }
* * *}
* * *... ... ...

}

but that still doesn't make $foo print "bar".

Thanks for any help on how to accomplish this.
Sep 8 '08 #2

P: n/a
briandichiara wrote:
Right now, i have found this solution by adding
extract($GLOBALS);
to the top of my function.

I would now like to somehow get the variables inside the include file
back into the global variable scope, making this function work just
like a normal include file. Is that possible?

On Sep 8, 11:35 am, briandichiara <briandichi...@gmail.comwrote:
>I'm trying to setup a class that will allow a file to be included on
the page. It goes something like:

<?php
class myClass {
... ... ...
function includeFile(){
if(file_exists($this->include_file)){
include($this->include_file);
} else {
$this->error("file missing");
}
}
... ... ...

}

$foo = "bar";

$page = new myClass();
$page->include_file = "the_file.php";
$page->includeFile();
?>

// the_file.php
<?php echo $foo; ?>

It has something to do with variable scope and accessing globals or
something, I'm not sure. My question is how should I setup my class so
that I do not have to use "return" at the bottom of any files I want
to include with my class? I want the includes to function just like
normal includes.

I tried

<?php
class myClass {
... ... ...
function includeFile(){
global $GLOBALS;
global $HTTP_SERVER_VARS;
if(file_exists($this->include_file)){
include($this->include_file);
} else {
$this->error("file missing");
}
}
... ... ...

}

but that still doesn't make $foo print "bar".

Thanks for any help on how to accomplish this.

You need to rethink your approach.

I see what you're trying to do - but sometimes classes are not the right
way to go. And this is one of them.

Leave your include() in the global scope and you won't have these problems.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 8 '08 #3

P: n/a
briandichiara wrote:
I'm trying to setup a class that will allow a file to be included on
the page. It goes something like:

<?php
class myClass {
... ... ...
function includeFile(){
if(file_exists($this->include_file)){
include($this->include_file);
} else {
$this->error("file missing");
}
}
... ... ...
}

$foo = "bar";

$page = new myClass();
$page->include_file = "the_file.php";
$page->includeFile();
?>

// the_file.php
<?php echo $foo; ?>

It has something to do with variable scope and accessing globals or
something, I'm not sure. My question is how should I setup my class so
that I do not have to use "return" at the bottom of any files I want
to include with my class? I want the includes to function just like
normal includes.

I tried

<?php
class myClass {
... ... ...
function includeFile(){
global $GLOBALS;
global $HTTP_SERVER_VARS;
if(file_exists($this->include_file)){
include($this->include_file);
} else {
$this->error("file missing");
}
}
... ... ...
}
but that still doesn't make $foo print "bar".

Thanks for any help on how to accomplish this.
I am not sure what you are trying to accomplish but this works.

myClass.php

<?php
class myClass
{
public function _include($include) {
require $include;
//error checking needed
}
}
?>

inc.php

<?php
global $result; // Important!
echo $result;
?>
test.php

<?php
$result = "It Worked";
require 'my_class.php'; // If in same directory as code base.
$class = new myClass();
$class->_include('inc.php');
?>

This is uber-basic code to show the basics. Make sure you are pointing
to the actual include file.

Good Luck

Scotty
Sep 8 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.