Re: Passing $_REQUEST by reference - sensible thing to do?



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);}

.



Relevant Pages

  • Re: OO problem: How to create a duplicate of a derived class with only a base variable ?
    ... by object reference: ... Does not use the TbaseClasses (or should it have been TbaseClassess? ... >> And it's not a class method. ... > as constants while instance methods aren't (because they'd need the ...
    (alt.comp.lang.borland-delphi)
  • Re: Using ref
    ... Hilton wrote: ... you/Jon/community as being pass by reference, ... They both use one level of indirection. ... pass-by-reference is all about the syntax. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Passing a procedure as a parameter?
    ... method or the reference to the class if the method is a class method. ... To make your TProcedure type compatible with methods declare it like ... It's like a whole big sucking thing ...
    (comp.lang.pascal.delphi.misc)
  • Re: segfault w/ block, but not file scope
    ... I repeat that C does not have pass by reference. ... >>> If you disagree, show me the part of the C Standard that supports your ... But it does support pass-by-reference as a programming ... C doesn't directly support pass-by-reference. ...
    (comp.lang.c)
  • Re: Another spinoza challenge
    ... "by reference" in C, you must use a special notation. ... That seems to be going too far to call it pass-by-reference. ... That's just a consequence of C being a systems programming language. ...
    (comp.lang.c)