include file in a class with global/parent scope?

include file in a class with global/parent scope?

am 14.06.2009 18:41:32 von Daniel Kolbo

Hello,

I understand the why $vars is not in the global scope, but i was
wondering if there was a way from within the class file to include a
file in the parent's scope?

i tried ::include('vars.php'), parent::include('vars.php'), but this
breaks syntax...

Please consider the following three files:

'scope.class.inc'
class CScope {

public $var = 'class scope\n';

function cinclude($filename) {
include('vars.php');
echo "In class $vars\n";
}
}
?>

'vars.php'
//global $vars;//if this line is uncommented then the desired result is
achieved however, i don't want to change all the variables to global
scope (this file is includeded in other files where global scoped
variables is not desireable).
$vars = 'vars.php scope\n';
?>

'scope.php'
include('scope.class.inc');
$object = new CScope;
$object->cinclude('vars.php');
echo "In original $vars\n";//$vars is not defined***
?>

Thanks,
dK

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

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

am 16.06.2009 13:12:14 von Daniel Kolbo

Martin Scotta wrote:
> Where is $vars? there is no $vars in your code...
>
> You can extract all the global space in the CScope method, it's quite
> simple, but less performant.
>
>
> class CScope {
>
> public $vars = 'class scope\n';
>
> function cinclude($filename) {
> extract( $GLOBALS );
> include('vars.php');
> echo "In class $vars\n";
> }
> }
>
> On Sun, Jun 14, 2009 at 1:41 PM, Daniel Kolbo > > wrote:
>
> Hello,
>
> I understand the why $vars is not in the global scope, but i was
> wondering if there was a way from within the class file to include a
> file in the parent's scope?
>
> i tried ::include('vars.php'), parent::include('vars.php'), but this
> breaks syntax...
>
> Please consider the following three files:
>
> 'scope.class.inc'
> > class CScope {
>
> public $vars = 'class scope\n';
>
> function cinclude($filename) {
> include('vars.php');
> echo "In class $vars\n";
> }
> }
> ?>
>
> 'vars.php'
> > //global $vars;//if this line is uncommented then the desired result is
> achieved however, i don't want to change all the variables to global
> scope (this file is includeded in other files where global scoped
> variables is not desireable).
> $vars = 'vars.php scope\n';
> ?>
>
> 'scope.php'
> > include('scope.class.inc');
> $object = new CScope;
> $object->cinclude('vars.php');
> echo "In original $vars\n";//$vars is not defined***
> ?>
>
> Thanks,
> dK
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
>
> --
> Martin Scotta

replace all $var with $vars. The extract method proposed is the
opposite of what i'm looking to do. I want to bring the class's include
scope into the calling object's scope.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

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

am 16.06.2009 13:15:59 von Daniel Kolbo

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'
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'
$vars = "hi";
?>

OUTPUT:
Notice: Undefined variable: vars in ...\test.php on line 10
obvious hi

DESIRED OUTPUT:
hi
obvious hi

Thanks,
dK

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

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

am 16.06.2009 18:02:09 von Shawn McKenzie

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'
> > 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'
> > $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
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
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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

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

am 16.06.2009 19:30:51 von Shawn McKenzie

Shawn McKenzie wrote:
> 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'
>> >> 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'
>> >> $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
> > 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
> > 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'
>
>

Another way off the top of my head:

//vars.php
$vars['something'] = 'hi';
$vars['whatever'] = 'bye';

//test.php
class CSomeClass {
var $vars;
public function cinclude() {
include('vars.php');
$this->vars = $vars;
}
}

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

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php