rss

Reference-Based Approach

Reference-based approach (passing-by-ref) often lead to wrong interpretation. question
common frequently asked questions related to this topic is, whether the reference increases
performance? In principle, the reference does not provide any performance advantage for
data types string, integer, and other scalar types. However, a different story if the reference
implemented on the combined data types (arrays and objects).

For more details, consider the following two examples of simple functions:
/ / Using reference
UseReferensi function (& $ a) {
     $ b = $ a;
     $ c = $ a;
}
/ / No reference
NoReferensi function ($ a) {
     $ b = $ a;
     $ c = $ a;
}


In fact, the PHP engine (Zend Engine) does not create duplicate variables when
to "pass by value", but menngimplementasikan reference-counted and copy-on-write
internally. Thus, in the first function, $ b and $ c take a longer time to
on-set, because the reference must be in-track. Meanwhile, $ b and $ c in the second function refers only to the
The original value of $ a, and counter-increment reference. The result, a second function to be executed more
faster than the first function.
On the other hand, when using a reference function that accepts an array or object parameters,
will improve performance. This is due, the type of data arrays and objects do not use
reference counting. In other words, if I use "pass by value" on an array or an object, then
will create multiple copies.
In addition, in PHP 5, all objects will automatically be passed by reference. So, you
no longer need to explicitly add the & operator.

No comments:

Post a Comment

 

Popular Posts

Followers