
01-13-2006, 02:03 AM
|
|
Junior Guru Wannabe
|
|
Join Date: Jul 2005
Posts: 77
|
|
PHP, Include and Variable scope
I ran into a little annoyance while working on a website in php... in a -very- simplified form this is what happens:
PHP Code:
// File: bar.php
$foo = "bar";
// do some stuff with $foo
PHP Code:
// File: foo.php
$foo = "foo";
include('bar.php');
// do some stuff with $foo
echo $foo; // prints "bar", but I want it to be "foo"
So basically my question is: What's the best way to isolate the scope of the included file so that variables in it don't interfere with the rest of the php code?
I know that include() inherits the scope depending on where it is called... so it would be possible to do something like this:
PHP Code:
function scope_fix() { include('bar.php'); }
scope_fix();
But that's not exactly very elegant... anyone know of a better/cleaner way to do this? Possibly with a different facility than include()?
|

01-13-2006, 03:08 AM
|
|
Web Hosting Guru
|
|
Join Date: Nov 2004
Location: HK
Posts: 309
|
|
just exchange their postition....
PHP Code:
include('bar.php');
$foo = "foo";
oherwise the value of $foo will be set to wt bar.php assign it
__________________
Rails in DA - Ruby on Rails plugin for Directdmin | DA-Tomcat - Tomcat Manager plugin for Directdmin
DA-PgSQL - PostgreSQL plugin for Directdmin | IP Deny Manager - IP Deny Manager plugin for Directdmin
DeeperAdmin - Manager your DirectAdmin server "deeper"
Order now at http://www.daplugin.com
|

01-13-2006, 03:26 AM
|
|
Junior Guru Wannabe
|
|
Join Date: Jul 2005
Posts: 77
|
|
Well that's the whole point.. I want to avoid that. Like I said, this is a really simplified example... there's a reason for the variables to be at the top in the original code and changing the order is not an option.
|

01-13-2006, 08:14 AM
|
|
Community Guide
|
|
Join Date: Jul 2003
Location: Kuwait
Posts: 5,100
|
|
There is no way to do this, unless you assign the variable a different name, or encapsulate it in a function or other defining scope.
__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise
|

01-13-2006, 08:19 AM
|
|
Junior Guru Wannabe
|
|
Join Date: Mar 2005
Location: New Zealand
Posts: 59
|
|
My solution is to include the variables and code within a function and then just call it straight after you have declared it. It keeps variables within the main code and this function separate.
You can however reference variables outside the function with the global keyword.
__________________
Hide website errors from your users plus manage all errors from multiple websites in one place FREE...
Codetrail.com: SSL Secure bug-tracking webservice.
|

01-13-2006, 08:25 AM
|
|
Retarded Noodleator
|
|
Join Date: Oct 2004
Location: Shimonoseki
Posts: 2,100
|
|
Is there any particular reason that it must be the same variable name?
__________________
Closed for winter...
|

01-13-2006, 10:25 AM
|
|
Aspiring Evangelist
|
|
Join Date: Aug 2005
Posts: 438
|
|
Quote:
|
What's the best way to isolate the scope of the included file so that variables in it don't interfere with the rest of the php code?
|
This is one of the weak point of php it lacks any sort of namespaces, which would solve these problems. Unfortunately you won't see namespaces until php 6! (oh well they may drop them again too though).
I really dont' know why they haven't solved this yet...its solved in the majority of scripting languages.
|

01-13-2006, 10:47 AM
|
|
Hail Eris !
|
|
Join Date: Oct 2002
Location: Canada
Posts: 3,100
|
|
Trolling is fun, isn't it.
include() behaves just the way it should. As you know, it is there to help you to include large chunk of php in current context and it does just that. If you have the same variable names in the included file, that is simply bad design. IMHO including anything but the config declarations, libraries and class definitions is bad design. If you really
need to do that you can make it look nicer by doing something like
include_local ('bar.php') ;
function include_local($incfile){
include ($incfile) ;
}
If you need to modify some variable from your main scope in the included file, you can pass those by reference to include_local() function so they get changed. It is not pretty and you should not do it, but, well it is doable.
|

01-13-2006, 01:03 PM
|
|
Aspiring Evangelist
|
|
Join Date: Aug 2005
Posts: 438
|
|
Quote:
|
Trolling is fun, isn't it.
|
Funny how its "trolling" to point out that the problem the poster is having is a long standing issue with php.
|

01-13-2006, 01:46 PM
|
|
Hail Eris !
|
|
Join Date: Oct 2002
Location: Canada
Posts: 3,100
|
|
Quote:
|
Originally Posted by Froggy
Funny how its "trolling" to point out that the problem the poster is having is a long standing issue with php.
|
It seems like it fits definition. You helped to no one and contributed nothing. Responding in details to your comment would take away from discussion at hand and we would argue are _s so much worse then .s and if namespaces are anything more then cure for laziness, and at which point module should become class and so on. If you wish such argument, make your own thread and see if someone responds.
Quote:
trolling:
Posting derogatory messages about sensitive subjects on newsgroups and chat rooms to bait users into responding.
|
|

01-13-2006, 02:23 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Jul 2005
Posts: 77
|
|
Thanks for the replies.
Quote:
|
Is there any particular reason that it must be the same variable name?
|
Purely aesthetic... in the original code an instance of an SQL handling class is created there with the name $sql... I don't want to be forced to use a different name for it every time I do that in a different file. And if things get moved around or changed in the future I don't want other variable names to cause issues like that either.
Quote:
|
include() behaves just the way it should. As you know, it is there to help you to include large chunk of php in current context and it does just that. If you have the same variable names in the included file, that is simply bad design. IMHO including anything but the config declarations, libraries and class definitions is bad design.
|
Yeah, I know it's doing exactly what it should do.. after all, if you do something like include('settings.php'), clearly you want the variable names to carry over. I was just asking whether there's a way around it or whether there's a different function that could be used to avoid it (on that note, it seems like virtual() would do just that, but it's only compatible with Apache).
As for bad design.. well, maybe you're right. I usually work with languages like C++ and not with php, so it's not my forte. I'll think about alternatives, but if nothing else the code you pasted should work well for me, thanks.
Last edited by MikeHart; 01-13-2006 at 02:27 PM.
|

01-13-2006, 02:58 PM
|
|
Community Guide
|
|
Join Date: Jul 2003
Location: Kuwait
Posts: 5,100
|
|
Quote:
|
Purely aesthetic... in the original code an instance of an SQL handling class is created there with the name $sql... I don't want to be forced to use a different name for it every time I do that in a different file. And if things get moved around or changed in the future I don't want other variable names to cause issues like that either.
|
You should not name your object one thing ($sql) and then use the same name elsewhere. This is not a problem with PHP, but rather with your design. You will run into the same problem in other languages such as Python (or others where the variable is not associated with a particular type).
Essentially, your problem -- removing the include portion:
$sql = new MyFunkySQLClass();
$sql->selectDatabase('myfunkydb');
/* some lines down */
$sql = "SELECT `foo` FROM `bar`";
/* later */
$sql->fetchResult(); //Whoops! $sql is no longer an instance of your class!
So just use different variable names, or as suggested -- create a scope by using functions, classes, etc.
__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise
|

01-13-2006, 10:53 PM
|
|
Aspiring Evangelist
|
|
Join Date: Aug 2005
Posts: 438
|
|
Quote:
|
if namespaces are anything more then cure for laziness
|
<sarcasm>Yes that is all they are a cure for lazinees nothing to do with name collisons etc</sarcasm>
|

01-13-2006, 11:44 PM
|
|
Hail Eris !
|
|
Join Date: Oct 2002
Location: Canada
Posts: 3,100
|
|
Quote:
|
Originally Posted by Froggy
<sarcasm>Yes that is all they are a cure for lazinees nothing to do with name collisons etc</sarcasm>
|
Ok .... and why would you have the same function names ?
|

01-14-2006, 12:31 AM
|
|
Aspiring Evangelist
|
|
Join Date: Aug 2005
Posts: 438
|
|
Quote:
|
Ok .... and why would you have the same function names ?
|
Because they have a similar function say as "read()", instead you're going to do something like "moduleA_read()" and "moduleB_read()" which is ugly, but this isn't the big issue. Also you cannot have two classes with the same name (which is fairly common).
The issue is that there is no standard matter of dealing with the namespace issue with php, some do what I did above some deal with it another way. If you are the only one working on a project and aren't using any outside "packages" this isn't so much an issue...but the minute you work in a team and start using outside "packages" its a big issue and name collisons become common place. This also makes code reuse difficult.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|