hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP, Include and Variable scope
Reply

Forum Jump

PHP, Include and Variable scope

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 01-13-2006, 02:03 AM
MikeHart MikeHart is offline
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()?



Sponsored Links
  #2  
Old 01-13-2006, 03:08 AM
hehachris hehachris is offline
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

  #3  
Old 01-13-2006, 03:26 AM
MikeHart MikeHart is offline
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.

Sponsored Links
  #4  
Old 01-13-2006, 08:14 AM
Burhan Burhan is offline
Community Guide
 
Join Date: Jul 2003
Location: Kuwait
Posts: 5,099
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

  #5  
Old 01-13-2006, 08:19 AM
NateD NateD is offline
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.

  #6  
Old 01-13-2006, 08:25 AM
BurakUeda BurakUeda is offline
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...

  #7  
Old 01-13-2006, 10:25 AM
Froggy Froggy is offline
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.

  #8  
Old 01-13-2006, 10:47 AM
sasha sasha is offline
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.

  #9  
Old 01-13-2006, 01:03 PM
Froggy Froggy is offline
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.

  #10  
Old 01-13-2006, 01:46 PM
sasha sasha is offline
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.

  #11  
Old 01-13-2006, 02:23 PM
MikeHart MikeHart is offline
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.
  #12  
Old 01-13-2006, 02:58 PM
Burhan Burhan is offline
Community Guide
 
Join Date: Jul 2003
Location: Kuwait
Posts: 5,099
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

  #13  
Old 01-13-2006, 10:53 PM
Froggy Froggy is offline
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>

  #14  
Old 01-13-2006, 11:44 PM
sasha sasha is offline
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 ?

  #15  
Old 01-14-2006, 12:31 AM
Froggy Froggy is offline
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.

Reply

Related posts from TheWhir.com
Title Type Date Posted
Rackspace Launches OpenStack e-Learning Course for On-Demand Training Web Hosting News 2013-09-25 17:10:12
Kim Dotcom Sues New Zealand Government over Illegal Raids, Spying Web Hosting News 2013-09-16 12:12:51
Strategies to Reduce Operating Costs, Avoid Email Blacklisting and Improve Deliverability Webinars 2013-10-16 15:35:21
Codero Launches Managed Hosting Services Catered to Small Businesses Web Hosting News 2013-02-21 16:28:14
Website Security Firm StopTheHacker Lands 10 Web Hosting Partners Web Hosting News 2012-06-07 16:51:37


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?