1

So aside from the obvious readability improvement (to me anyway), is there a performance benefit gained in PHP when assigning a local variable to the value of an element in a superglobal array, versus accessing the element's value directly?

For example, given a class that repeatedly uses: $_SERVER['REQUEST_URI'] as an argument for a number of functions, is there a performance benefit to creating a class property (or for that matter a local variable with class scope) and using the variable directly? I have a C# background and I have learned from experience that using the local variable does offer a performance benefit.

$url = $_SERVER['REQUEST_URI'];

public function doSomething() {
echo $this->url;
}

public function doSomethingElse() {
echo $this->url;
}

versus

public function doSomething() {
echo $_SERVER['REQUEST_URI'];
}

public function doSomethingElse() {
echo $_SERVER['REQUEST_URI'];
}
Elliot Rodriguez
  • 608
  • 6
  • 25
  • 1
    I would argue that if you are using a class in the first place then you should be using a property for encapsulation's sake. I wouldn't worry about microoptimisations such as this because a) performance gains would be virtually unnoticeable and b) you're already using PHP classes. Most optimisation in any PHP application is done by reducing redundant function invocations and calls on unindexed DB fields, etc. – fisk Sep 16 '15 at 13:45

2 Answers2

1

From PHP Docs:

By default, variables are always assigned by value. That is to say, when you assign an expression to a variable, the entire value of the original expression is copied into the destination variable. This means, for instance, that after assigning one variable's value to another, changing one of those variables will have no effect on the other. For more information on this kind of assignment, see the chapter on Expressions.

PHP also offers another way to assign values to variables: assign by reference. This means that the new variable simply references (in other words, "becomes an alias for" or "points to") the original variable. Changes to the new variable affect the original, and vice versa.

That said, unless you're using reference assignment, both examples are the same thing (except that you're using an almost irrelevant amount of extra memory in the first one, having two vars with the same value).

al'ein
  • 1,711
  • 1
  • 14
  • 21
  • 3
    Just an addition: PHP will not copy the struct of the variable while assigning it by value, it will increase a reference counter instead. From the developer's perspective it seems to be a copy. An actual copy will be made when changing the value of the variable. – Pred Sep 16 '15 at 13:42
  • Good point, but then won't be a copy anymore, since its value was changed. – al'ein Sep 16 '15 at 14:13
  • Actually the process is: clone the original struct, then modify the data (or overwrite it on the fly), so yes, it is a copy. – Pred Sep 16 '15 at 14:20
  • 1
    Note the mechanism is called [copy-on-write](http://stackoverflow.com/a/11075245/2908724) – bishop Sep 16 '15 at 14:33
0

It depends both on PHP version and how you've declared the member variable.

In PHP <= 5.3, the general answer is no: both are container accesses, which PHP implements as a hash table look-up in constant time. In the object property case, your container is $this. In the superglobal case, your container is $_SERVER.

In PHP >= 5.4, the answer is maybe. When you declare the proeprty, PHP optimizes the memory footprint and access path. When you don't declare the property, the look-up is a hash table scan, and therefore both approaches are equally performant.

So, it's fastest to declare your properties.

bishop
  • 37,830
  • 11
  • 104
  • 139