473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Including file Inside class

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
3 2444
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Thanasis \(sch\) | last post by:
Hello to everyone, i'm new to css and i have a question. i have an external css (style.css) , that i link in all html pages. inside the css file there is a class detais declared as follows:...
11
by: cppaddict | last post by:
Say that your CustomClass.h header files requires #include <string> Now say that your CustomClass.cpp file also requires string. Is it good form to repeat the <string> include to make the...
1
by: Arnaud Debaene | last post by:
Hello, I think I found a bug in VC 7.1 concerning destruction of stack objects when linking a static, non managed, C++ library within a managed C++ application. Here is a repro case : 1)...
3
by: AK | last post by:
I'm using a .NET Windows Forms Applications project. I'm using LoadLibrary & GetProcAddress to use a DLL function. This is all done in the main cpp file. I would like to use this DLL function in a...
6
by: Al-Burak | last post by:
I have a class which only purpose is to provide services to a variety of classes in other files. The 'manipulator' class is aware of the other classes only because the header files have been...
4
by: 'Mani | last post by:
Hi, This is just a generic question, where i want to know what is the difference in including a header file in a .h file and .cpp file. I have a class called MyClass (MyClass.h & MyClass.cpp)....
11
by: Gary Wessle | last post by:
Hi is it right to have a line like #include <path/to/header.hfor a library on my system, in my header file and use some functions provided by this library in the implementation file (file.cpp)...
1
by: mxnewby | last post by:
What instructions must I include in my program to be able to use clases that derive from superclases contained in separate files. In other words, if class B is a subclass of class A, but the former...
21
by: FutureShock | last post by:
I have just recently started to use OOP for my web applications and am running into some head scratching issues. I wanted to have a separate file for all my configuration variables. Some of them...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.