Re: Including file Inside class



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
.



Relevant Pages

  • Including file Inside class
    ... I'm trying to setup a class that will allow a file to be included on ... <?php echo $foo; ?> ... It has something to do with variable scope and accessing globals or ...
    (comp.lang.php)
  • Re: copy of object is a reference in PHP5
    ... myObj Object ... All objects in PHP 5, if I'm not mistaken, are always passed by ... class MyClass ... $class1 = new MyClass; ...
    (comp.lang.php)
  • Using classes as namespaces: good style?
    ... Is it considered good style to use classes as namespaces in PHP (that ... class MyClass ... class YourClass ... Reason is: I've got several functions that do almost the same thing, ...
    (alt.php)
  • Re: what is the -> operator?
    ... PHP manual at php.net, I can't find a description of what it does. ... class foo ... If I have a class myClass with methods foo and bar ...
    (comp.lang.php)
  • Re: Including file Inside class
    ... On Sep 8, 11:35 am, briandichiara wrote: ... <?php echo $foo; ?> ... It has something to do with variable scope and accessing globals or ...
    (comp.lang.php)