Opened 15 years ago
Closed 15 years ago
#11663 closed defect (bug) (worksforme)
Return by Referenced missed - PHP4 Compat
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 2.9 |
Component: | General | Keywords: | dev-feedback |
Focuses: | Cc: |
Description
There might be various points where is not properly taken care of the PHP 4 compability. One part is to take care of passing/returning objects be reference to/from functions.
Before sniffing the overall code, I create this ticket for reference and to provide first examples so that a decision can be made, how to deal with / merge patches etc. .
Pass by Reference
function parameters that accept objects need to set as pass by reference:
function foo(&$var)
Return by Reference
Functions that return objects need to be set as return by reference so that the actual object and not a copy is returned in php 4:
function &bar() { $obj = new Object(); return $obj; }
Change History (10)
#2
@
15 years ago
- Keywords dev-feedback 2nd-opinion added
I need to get feedback from core-devs wether or not and if yes how those issues should be handeled.
#3
@
15 years ago
Core-Dev Statement needed. Please decide. Coding of many patches is based on this.
Related: #11780
#4
follow-up:
↓ 5
@
15 years ago
can you supply some references for why References should be returned for PHP4? As far as i'm aware, for simple objects there are no real issues..
Objects are returned in a lot of locations, for example, WP_Error. There are only a few specific locations where references are returned (Users/capabilities/Roles) where they are needed due to the object being modified by the calling functions. Most other objects in WordPress represent something and do not change, as far as i'm aware of, cloning such objects shouldnt cause an issue? The local object to the function should be cleaned up by the builtin garbage collection when the function goes out of scope.
#5
in reply to:
↑ 4
@
15 years ago
Replying to dd32:
can you supply some references for why References should be returned for PHP4?
Shure, it's common practise for WP core code. For more Details please see my answer to your comment in #11780 . This ticket here is to gather Core-Dev feedback in form of a statement. I would love to not see much discussion in here. Just to keep things tight.
#7
@
15 years ago
- Milestone 3.0 deleted
- Resolution set to worksforme
- Status changed from new to closed
Cross-posting from #11780 :
In PHP there is no such thing as "return by reference" by the normal definition. PHP always uses references for return variables. The only exception is when there's a scope conflict, as in, return $GLOBALS['abc'];
or return $this->var;
when PHP implicitly returns by value.
Most of the examples cited by the other ticket were probably returning cached objects, which would be maintained as separate copies unless explicitly referenced when returned.
plugins_api() returned a local variable and needed no special treatment.
I am not aware of any change in this behavior between PHP4 and PHP5.
Enjoy!
#8
@
15 years ago
- Resolution worksforme deleted
- Status changed from closed to reopened
Miqrogroove, can I read your comment as the per-se core-dev feedback on the issue? I think not, since this ticket has been opened to explicitly ask for core-dev feedback on the issue, it's still valid and still opened.
I would love to see you taking more care when you actually close those tickets and read them in full.
#9
@
15 years ago
After a chat with DD32 the result is: It's undocumented / unspecified when to return by reference and when not. It depens on the concrete situation and a core developer will know when to use it or not.
An example where it's a need to return by reference is for caching objects because otherwise they would be copied on write and it would not be possible to write into the cache object.
Only returning an object in a function is not per-se the requirement to return by reference within wordpress.
For general and in depth PHP variable reference related information, I've put a blog post online.
I miss a return by reference in current_theme_info(): See Change in this Patch