Skip to content

Overhaul Functions/Methods To Use Named Arguments #27

@Toxocious

Description

@Toxocious

With the release of PHP >8.x, named arguments have been added to the spec.

We can make use of them - primarily for functions and methods with a number of optional arguments - to make calling them easier to read.

Once the current refactoring of how pages are handles has been completed (see #14 #15), focus should be shifted to updating core functions, methods, and classes, and to implement usage of named arguments for applicable function calls.

Example

<?php
    function NamedArgsTest($username, $email = null, $role = 'subscriber', $active = true) {
        return [
            'username' => $username,
            'email'    => $email,
            'role'     => $role,
            'active'   => $active
        ];
    }

    $NamedArgsOutput = NamedArgsTest(
        username: "john_doe",
        role: "admin"
    );

    var_dump($NamedArgsOutput);

This outputs the following:

array (size=4)
  'username' => string 'john_doe' (length=8)
  'email' => null
  'role' => string 'admin' (length=5)
  'active' => boolean true

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestgood first issueGood for newcomersrefactorDesirable refactoring of the feature/script in the issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions