Jump to content

The Keyword: USE


iwato

Recommended Posts

QUESTION: A fairly thorough search of the PHP Manual has brought me little closer toward understanding the use of the keyword use in the following code. I found the word in PHP's list of reserved words, but clicking on it referred me to namespaces. I don't get it.What's more, I do not understand why one cannot just right function($quantity, $product, $tax, &$total) and achieve the same effect.

public function getTotal($tax) {			$total = 0.00;					$callback = function ($quantity, $product) use ($tax, &$total) {				$pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product));				$total += ($pricePerItem * $quantity) * ($tax + 1.0);			};					array_walk($this->products, $callback);			return round($total, 2);		}

Roddy

Link to comment
Share on other sites

I'm not certain, but I believe it's using the object's properties $this->tax and $this->total. Which would be like passing those values as parameters when the function is called.

Link to comment
Share on other sites

I'm not certain, but I believe it's using the object's properties $this->tax and $this->total. Which would be like passing those values as parameters when the function is called.
As the method of an instantiated object, the method takes only one parameter -- namely, $tax. The variable called $total is completely contained within the scope of the method and the $callback() function. It appears nowhere else in the class definition.Roddy
Link to comment
Share on other sites

Unlike similar languages (C++, JavaScript), variables are only available in the scope they're declared. Other languages allow you to use them in inner scopes too. That's why you don't see "use" in JavaScript.In PHP though, if you wanted to use the value of the $tax variable within the inner function, you... can't. Simply writing $tax as an argument would create a new variable that's available only in that scope with a value that's supplied at call time, using $tax directly in the body would also define a new variable specifically for that scope (not overridable at call time), and using "global $tax" would assume a variable in the global scope, not in the parent function's scope.To overcome this limitation, and achieve something similar as to how JavaScript handles variables in parent non-global scopes, the "use" keyword is introduced to only add specific variables to that inner scope.

Link to comment
Share on other sites

Unlike similar languages (C++, JavaScript), variables are only available in the scope they're declared. Other languages allow you to use them in inner scopes too. That's why you don't see "use" in JavaScript.In PHP though, if you wanted to use the value of the $tax variable within the inner function, you... can't. Simply writing $tax as an argument would create a new variable that's available only in that scope with a value that's supplied at call time, using $tax directly in the body would also define a new variable specifically for that scope (not overridable at call time), and using "global $tax" would assume a variable in the global scope, not in the parent function's scope.To overcome this limitation, and achieve something similar as to how JavaScript handles variables in parent non-global scopes, the "use" keyword is introduced to only add specific variables to that inner scope.
thanks for your post. It enlightened me on how scoping works in PHP. Good to get some clarity before I resume on one of my applications. Combined with iwato's post on USE, I think I can figure it out!
Link to comment
Share on other sites

When PHP decided to add anonymous functions, they realized the anonymous functions wouldn't be useful in their full potential unless closures were possible (which is one of the most common uses and benefits of having anonymous functions). This created a problem because PHP's scope is so strict, so they added "use()" to tell the function which variables to take into its scope. For example:JS:

var a = 1;var b = function() {   document.write(a);};

The same function expressed in PHP:

$a = 1;$b = function() use($a) {   echo($a);};

Javascript doesn't have the strict scope of PHP, so variables declared in the parent scope are allowed to trickle down in a sense.

Link to comment
Share on other sites

When PHP decided to add anonymous functions, they realized the anonymous functions wouldn't be useful in their full potential unless closures were possible (which is one of the most common uses and benefits of having anonymous functions). This created a problem because PHP's scope is so strict, so they added "use()" to tell the function which variables to take into its scope. For example:JS:
var a = 1;var b = function() {   document.write(a);};

The same function expressed in PHP:

$a = 1;$b = function() use($a) {   echo($a);};

Javascript doesn't have the strict scope of PHP, so variables declared in the parent scope are allowed to trickle down in a sense.

Exactly what I was thinking. My job's developing environment is pretty much exclusive to Javascript, and I was having trouble trying to make sense of scoping in PHP, getting confused with having to use global and whatnot, which didn't seem right. This thread makes it all the more clearer. Thanks for you contribution, it has helped me immensely.
Link to comment
Share on other sites

To ... achieve something similar as to how JavaScript handles variables in parent non-global scopes, the "use" keyword is introduced to only add specific variables to that inner scope.
I would like to thank both boen_robot and Dilated for their useful comments and example, respectively. By the way, I have since discovered a reference to use-variables in PHP's WIKI -- a resource that I did not even know existed until now.Also, in passing I discovered that my unanswered question "What's more, I do not understand why one cannot just write function($quantity, $product, $tax, &$total) and achieve the same effect." was misplaced for the simple reason that the structure of the call-back function of the array-walk() function is restricted, and that additional parameters necessary to execute the call-back function must be entered via the array-walk() function's $user_data parameter.Roddy
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...