Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 6 years ago

#40332 closed enhancement (invalid)

Is passing arguments by reference in Walker_Nav_Menu methods needed?

Reported by: dingo_d's profile dingo_d Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.3
Component: Menus Keywords:
Focuses: performance Cc:

Description

From what I've read, since PHP 5 objects are automatically passed by reference, and in the Walker_Nav_Menu class we have methods that have arguments that are passed by reference like

public function start_lvl( &$output, $depth = 0, $args = array() ) {
        if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
                $t = '';
                $n = '';
        } else {
                $t = "\t";
                $n = "\n";
        }
        $indent = str_repeat( $t, $depth );
        $output .= "{$n}{$indent}<ul class=\"sub-menu\">{$n}";
}

So my question is: is this necessary? Especially given the fact that wordpress.org recommends the users to install it on PHP7 or greater.
Is this because of backwards compatibility or?

Change History (2)

#1 @welcher
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

@dingo_bastard thanks for the ticket! The pass by reference operator allows the function to modify the passed variable without returning it.

See the example below.

class Reference_Test {
	public function pass_by_reference( &$output ) {
		$output .= ' : Passed By Reference';
	}
	
	public function no_reference( $output  ) {
		$output .= ' : No Reference';
	}
}

$output = 'Start';
$test   = new Reference_Test();

$test->pass_by_reference( $output );
$test->no_reference( $output );

echo $output; // Output is "Start : Passed By Reference"

#2 @dd32
6 years ago

  • Reporter changed from dingo_bastard to dingo_d
Note: See TracTickets for help on using tickets.