-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parameter 1 to gutenberg_register_packages_styles() expected to be a reference, value given #19403
Comments
…re to fix a PHP warning This closes WordPress#19403 and eliminates a PHP 4.x compatibility fix that causes problems
I'm a bit confused about this. It seems to be implied in this issue that prefixing the
https://www.php.net/manual/en/functions.arguments.php#functions.arguments.by-reference As to whether Gutenberg needs a reference value, it's unclear. I expect this is carried over from how core handles default script enqueues (possibly what you're referencing):
Could you clarify:
|
I'd say these are carried over from Core, removing them in my local instance eliminates the PHP warnings without impacting the behaviour. Of note this is also an issue in Core, and there's a Trac ticket for it.
If I have the following code: function makeCall( $obj ) {
// ....
}
$foo = new Test();
makeCall( $foo ); Then yes, the variable So in PHP 4, it's the object itself that's copied, leading to duplication. In PHP 5, it's the reference that's copied, so it isn't the same reference, but it points to the same object. This is how object variable assignments work in PHP 5+ by default as a result of how they work with variables, it's non-objects that are copied on assignment such as strings or arrays. Here's a program to demonstrate, we'll create an object with a counter, and increment it by passing the object into a function: <?php
class Test {
public $counter = 1;
}
function increment( Test $obj ) {
$obj->counter += 1;
}
$foo = new Test();
$foo->counter = 1;
echo $foo->counter . "\n";
increment( $foo );
echo $foo->counter . "\n";
increment( $foo );
echo $foo->counter . "\n";
increment( $foo );
echo $foo->counter . "\n";
increment( $foo );
echo $foo->counter."\n"; Output: ❯ php references.php
1
2
3
4
5
~/dev
❯ In general it's best to avoid references in modern PHP as they aren't necessary, and because they aren't well understood. For example, in this PHP code, what do you think the output will be? $a = 1;
$b = &$a;
$a = 2;
echo $b; Most people would say |
This was fixed in #21987. |
Describe the bug
In PHP v4, doing this meant copying an object:
Now $foo and $bar are not the same, a copy has been made, and 2 objects exist.
To get around this, WP used references, e.g.:
Now
$bar
references$foo
, which is the original object. This is why old WP code has&$this
when adding object methods to hooks and filters.This was silly and the PHP engine devs rightly changed this to pass by reference by default in PHP 5, allowing
array( $this, 'method' )
to work without lots of duplicate objects being created.In PHP 5,
$foo
does not contain the object, but rather a reference to the object, so it can be assigned like normal variables without the bug happening.Sadly, some of these PHP 4 compat hacks is still in core in the style and scripts system, and have been copy pasted into Gutenberg generating additional PHP warnings in some environments:
Here's the function in question:
Notice that to avoid duplicating the WP_Styles object in PHP 4, a reference is passed, even though it isn't necessary.
The problem goes away when the ampersand is removed:
There are numerous other functions in that file that have the same problem and generate the same warning
To reproduce
Steps to reproduce the behavior:
The text was updated successfully, but these errors were encountered: