|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
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.
|
|
#2
|
|||
|
|||
|
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.... |
|
#3
|
|||
|
|||
|
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 :-/ |
|
#4
|
|||
|
|||
|
RE: RE: Variable Scope Problems
Quote:
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. |
|
#5
|
|||
|
|||
|
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... |
|
#6
|
|||
|
|||
|
RE: Variable Scope Problems
|
|
#7
|
|||
|
|||
|
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? |
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > Variable Scope Problems |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|