[wp-hackers] Passing class methods by reference
Jacob Santos
wordpress at santosj.name
Thu Aug 5 13:21:43 UTC 2010
Actually for plugins, it is better to always reference when passing an
object in an array. Classes aren't passed automatically by reference as
in to the area of memory, but reference to the handle (Sara Golomen has
a really good article or was it the Release Master of PHP5.2?).
Therefore, you can still change the object in $this without changing the
original, whereas &$this will. When using objects in plugins in
WordPress, the object should always be referenced explicitly in order to
assure that it can be removed in the future.
Jacob Santos
On 8/4/2010 11:06 AM, Dan Gayle wrote:
> I see some plugins that have a callback to a class method by reference like so:
>
> array(&$this, 'function_name' ));
>
> The codex says "if the function is a member of a class within the
> plugin it should be referenced as array( $this, 'function_name' )"
>
> I assume that passing a reference is an attempt to save memory, but in
> PHP 5 aren't objects automatically passed by reference? What is the
> benefit, if any, of this style of callback?
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
More information about the wp-hackers
mailing list