You are here: Re: Execute code in global scope « PHP Programming Language « IT news, forums, messages
Re: Execute code in global scope

Posted by Tommy Gildseth on 02/24/06 22:27

m.epper@gmail.com wrote:

>
> The problem is that this code will be run from eval which is inside
> another function.
> In this case the php engine will parse something like this:
>
> <?php
> function myFunction($code){
> $var="Hello";
> function myFunction2(){
> global $var;
> $var.=" World";
> }
>
> myFunction2();
> echo $var;
> }
> myFunction($code);
> ?>
>
> Here the output will be only "Hello" because myFunction2 (which should
> add " World" to $var) will try to change the value of $var, a global
> variable that doesn't exists!.


Your problem here, is that the variable $var isn't in the global scope to
begin with, it's in the scope of the function myFunction, and even though
myFunction2() is created within myFunction(), it does not inherit
myFunction()'s scope or variables.

The only solution that I can think of right now, would be if you both in
myFunction() and in the eval'ed code containing myFunction2() refer to the
variables via the $GLOBALS array instead of directly using the variable
name.

--
Tommy
http://design.twobarks.com/

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация