XF 2.2 Cannot use a positional argument after a named argument

KSA

Well-known member
Trying to learn from the tutorial Kier posted in the Have you seen this forum but I go stuck when attempting to do this:

PHP:
return $this->view( viewClass: 'KSA\Pad:Note\Test', templateName: 'ksa_pad_test', $viewParams);

I get this error:

Cannot use a positional argument after a named argument.

PHP 8
 
So based on what you've written above, I'm pretty sure you're on this video, roughly at this point:

To view this content we will need your consent to set third party cookies.
For more detailed information, see our cookies page.

I'm not sure if you have intentionally or unintentionally used some fairly new PHP syntax here... But note in the preceding video that Kier does not type the viewClass: or templateName: bits but the IDE adds them automatically. This isn't actually code that the IDE writes into the file, they are just hints so you know the name of the argument you're typing. It helps add useful context when writing and later reading the code, but those labels are just labels and are not required in the code.

With that in mind, your code should look something like this. This is what Kier's code would have looked like if the PhpStorm feature that adds those hints was disabled:

PHP:
return $this->view('KSA\Pad:Note\Test', 'ksa_pad_test', $viewParams);

Now, I did say "intentionally or unintentionally" because what you've actually written is mostly valid since PHP 8.0 and that is a feature called named arguments.

This is where rather than relying on the assumption that all arguments are specified in the same order as defined in the signature of the method you're calling, you can actually define those arguments in any order if you prefix them with the argument name, e.g. this would be valid too:

PHP:
return $this->view(templateName: 'ksa_pad_test', viewClass: 'KSA\Pad:Note\Test');

Or it could be used to omit certain arguments entirely:

PHP:
return $this->view(templateName: 'ksa_pad_test');

If you are intending to use named arguments (I don't recommend this) then to fix the existing code you would need to change it to:

PHP:
return $this->view( viewClass: 'KSA\Pad:Note\Test', templateName: 'ksa_pad_test', params: $viewParams);

Instead, I'd recommend sticking with what Kier wrote which, without the "helpful" labels added by PhpStorm actually looked like:

PHP:
return $this->view('KSA\Pad:Note\Test', 'ksa_pad_test', $viewParams);

Hope that helps :)
 
So based on what you've written above, I'm pretty sure you're on this video, roughly at this point:

To view this content we will need your consent to set third party cookies.
For more detailed information, see our cookies page.

I'm not sure if you have intentionally or unintentionally used some fairly new PHP syntax here... But note in the preceding video that Kier does not type the viewClass: or templateName: bits but the IDE adds them automatically. This isn't actually code that the IDE writes into the file, they are just hints so you know the name of the argument you're typing. It helps add useful context when writing and later reading the code, but those labels are just labels and are not required in the code.

With that in mind, your code should look something like this. This is what Kier's code would have looked like if the PhpStorm feature that adds those hints was disabled:

PHP:
return $this->view('KSA\Pad:Note\Test', 'ksa_pad_test', $viewParams);

Now, I did say "intentionally or unintentionally" because what you've actually written is mostly valid since PHP 8.0 and that is a feature called named arguments.

This is where rather than relying on the assumption that all arguments are specified in the same order as defined in the signature of the method you're calling, you can actually define those arguments in any order if you prefix them with the argument name, e.g. this would be valid too:

PHP:
return $this->view(templateName: 'ksa_pad_test', viewClass: 'KSA\Pad:Note\Test');

Or it could be used to omit certain arguments entirely:

PHP:
return $this->view(templateName: 'ksa_pad_test');

If you are intending to use named arguments (I don't recommend this) then to fix the existing code you would need to change it to:

PHP:
return $this->view( viewClass: 'KSA\Pad:Note\Test', templateName: 'ksa_pad_test', params: $viewParams);

Instead, I'd recommend sticking with what Kier wrote which, without the "helpful" labels added by PhpStorm actually looked like:

PHP:
return $this->view('KSA\Pad:Note\Test', 'ksa_pad_test', $viewParams);

Hope that helps :)

Reading this and reflecting on the logic, it makes sense now. Fixed that and it works now.

Thanks for the input.

bookmarked for future reference.
 
Top Bottom