Re: [PHP] include file in a class with global/parent scope?

[email protected] (Shawn McKenzie)
Newsgroups php.general
Message-ID <[email protected]>
Daniel Kolbo wrote:
> Hello,
> 
> I've cleaned up my question a bit.
> 
> I want the included file which is called within a method of a class to
> have the same scope as the instantiation of the class's object.  That
> is, i want a class to include a file in the calling object's scope.  How
> would one do this?
> 
> 'test.php'
> <?php
> class CSomeClass {
>  public function cinclude() {
>    include('vars.php');
>  }
> }
> 
> $object = new CSomeClass;
> $object->cinclude();
> echo $vars;//i want this to print 'hi'
> include('vars.php');
> echo "obvious $vars";
> ?>
> 
> 'vars.php'
> <?php
> $vars = "hi";
> ?>
> 
> OUTPUT:
> Notice: Undefined variable: vars in ...\test.php on line 10
> obvious hi
> 
> DESIRED OUTPUT:
> hi
> obvious hi
> 
> Thanks,
> dK

Should get you started:

//one way
<?php
class CSomeClass {
 public function cinclude() {
   include('vars.php');
   return get_defined_vars();
 }
}

$object = new CSomeClass;
$inc_vars = $object->cinclude();
echo $inc_vars['vars'];//i want this to print 'hi'

---or---

//another way
<?php
class CSomeClass {
 var $inc_vars;
 public function cinclude() {
   include('vars.php');
   $this->inc_vars = get_defined_vars();
 }
}

$object = new CSomeClass;
$object->cinclude();
echo $object->inc_vars['vars'];//i want this to print 'hi'


-- 
Thanks!
-Shawn
http://www.spidean.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.