PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old May 31st, 2002, 06:49 AM
Zoombie Zoombie is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Boston, MA, US
Posts: 19 Zoombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Zoombie Send a message via AIM to Zoombie
Variable Scope Problems

Does anybody know of a way to make a function use variables from the scope in which it was called? I'm trying to make a template function, which takes a string from a database and parses variable names in the HTML. Unfortunately, I currently have to return the code to complete this operation from the function and use eval with every call.

Reply With Quote
  #2  
Old May 31st, 2002, 02:11 PM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: Variable Scope Problems

Whatever variables you need to use in the function, you can make global. Let's say you want to access var2,var5,and var6 in your function..stick:

global var2,var5,var6;

at the top of your function, and then you can get at them....


Reply With Quote
  #3  
Old May 31st, 2002, 05:46 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: Variable Scope Problems

i spoke with him on instant messenger a little about this.. the global would work, but it's not exactly what he's trying to do. he wants to use the variables from within the scope that they were originally assigned, if that makes sense.

i'm not sure of how to go about it myself :-/

Reply With Quote
  #4  
Old June 1st, 2002, 05:19 AM
Zoombie Zoombie is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Boston, MA, US
Posts: 19 Zoombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Zoombie Send a message via AIM to Zoombie
RE: RE: Variable Scope Problems

Quote:
Whatever variables you need to use in the function, you can make global. Let's say you want to access var2,var5,and var6 in your function..stick:

global var2,var5,var6;

at the top of your function, and then you can get at them....


The problem is that I need to access variables from the calling scope. If the function is called from the global scope, it uses a global. If the function is called from a function, it uses that function's scope.

Reply With Quote
  #5  
Old June 2nd, 2002, 11:41 AM
dwd dwd is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: London, UK
Posts: 13 dwd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Variable Scope Problems

PHP doesn't support that, IIRC.

Smalltalk does to a degree, I think, since you have access to the calling object, although local variables wouldn't be accessible, unless your program changed the scoping rules inside Smalltalk - given that it's Smalltalk, that's probably possible.

JScript/Javascript/ECMAScript certainly does, since there's no difference in between block-scoping rules and call-scoping rules, unlike almost any oter language. Most of ECMAscript programming seems to be spent in avoiding accidentally using that "feature".

But back to PHP...

For what I think you're trying to do, you probably want to consider placing the variables into an array for translation. Although it's more work for you, it'll also prevent accidentally sending out internal variables which you didn't intend to...

Reply With Quote
  #6  
Old June 4th, 2002, 08:15 PM
greggory greggory is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Reims, France
Posts: 82 greggory User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Variable Scope Problems

Can't you name your variables with the scope thanx to variable variables, assuming that for a function the scope are its parameters....

Example:
php Code:
Original - php Code
  1.  
  2. <?php
  3. function my_fct( $a, $b )
  4. {
  5.   global $xx;
  6.   global $local_xx[$a][$b];
  7.  
  8.   // Get and parse $xx
  9.  
  10.   $local_xx[$a][$b] = $xx;
  11. }
  12. ?>


Unfortunately, U still need a global $xx..

Reply With Quote
  #7  
Old June 5th, 2002, 01:01 AM
Smiley Smiley is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Australia
Posts: 30 Smiley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Variable Scope Problems

Assuming you are using objects, why not just pass the object across the function as well. Then all you have to do is use the variables like normal

$object->variable

That uses the scope of that object doesn't it?

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Variable Scope Problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway