Re: Passing $_REQUEST by reference - sensible thing to do?
- From: Curtis Dyer <dyer85@xxxxxxxxx>
- Date: Fri, 19 Jun 2009 11:02:45 GMT
On 19 Jun 2009, <news.freedom2surf.net> wrote:
Hi there,
I've starting using PHP5 a lot more and making use of classes. I
think I come a bit from the old school of programming trying to
save as much CPU time as possible
In most scenarios, even in lower-level languages, this is premature
optimization. In my experience, it's better to focus on writing
clear and maintainable code, rather than shave a few cycles. In
any case, in a high-level language like PHP, you can't even
guarantee you buy much from most premature optimizations.
and I have made the assumption
that if I'm calling a method of one of my classes that I want to
do work on the $_REQUEST variables,
rather then pass the object to my class method, instead I pass it
by reference.
Perhaps you're thinking of C terminology; `$_REQUEST' is a
superglobal, which is an associative array. It is not an object.
In most cases, you should not use `$_REQUEST', you should
explicitly be using `$_POST', `$_GET', or `$_COOKIE', depending on
which you actually need. The main problem is that the order in
which values appear in the superglobal is not necessarily portable,
as it inherits the settings from the variables_order php.ini
configuration.
My unconfirmed assumption is that this will save a fraction of
time because a copy of the $_REQUEST object will not be necessary
and therefore there is less overhead in my function call.
Is my assumption right?
I believe it may, in extreme cases, but in general, you should only
use pass-by-reference when it makes sense to do so.
I strongly suggest you read php.net's section on references:
<http://php.net/manual/en/language.references.php>
Each sub-section has highly valuable information.
E.g.
class example () {
public function example1 (&$request_vars) {
//process $_REQUEST
}
}
When you pass-by-reference, keep in mind the possible side effects.
Again, given you've understood PHP's references, it's best to use
them where they make sense, not to optimize.
Also, very important to remember, in PHP 5, you should not pass
objects by reference, unless you really know what you're doing,
because objects' object identifiers are passed by value, not the
object itself.
<http://php.net/manual/en/language.oop5.references.php>
--
~Curtis
Anonymous (1984 IOCCC winner):
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
.
- References:
- Passing $_REQUEST by reference - sensible thing to do?
- From: news.freedom2surf.net
- Passing $_REQUEST by reference - sensible thing to do?
- Prev by Date: Re: comma in string
- Next by Date: Re: Passing $_REQUEST by reference - sensible thing to do?
- Previous by thread: Re: Passing $_REQUEST by reference - sensible thing to do?
- Next by thread: Re: Passing $_REQUEST by reference - sensible thing to do?
- Index(es):
Relevant Pages
|