WordPress / WordPress/WordPress-Coding-Standards

Forbid the addition of new magic class methods

Open
#2,445 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Status: Needs investigation Type: Enhancement
Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

Is your feature request related to a problem?

Adding new magic methods such as __get and __set to WordPress Core should be discouraged unless necessary to fix existing dynamic class properties.
There is no good reason to use magic methods unless someone is writing an ORM library (which is not likely the case with WordPress Core).
PHP classes can almost always function just fine without them.

There have also been relatively recent examples of adding __get magic methods to new classes in WordPress versions 6.1 and 6.5 to provide backward compatibility. However, I'm not sure if that qualifies as a good reason to use magic methods in entirely new code.

Describe the solution you'd like

There could be a sniff that can take care of that (PrefixAllGlobals comes to mind).

class WP_Class {
    public function __get( $name ) { // This and similar magic methods should be detected and throw a linter error. Existing magic method usages should be allowed if necessary to fix existing dynamic class properties.
    }
}

I'm fine with this issue being closed without long explanations, as this idea may have already been explored and rejected.

Additional context (optional)

  • I intend to create a pull request to implement this feature.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing PrefixAllGlobals sniff, which the issue identifies as a possible model for detecting magic methods. Determine how the proposed rule should distinguish new magic methods from existing compatibility-related usages, then verify that the intended behavior is covered by the repository's sniff tests; the issue does not name specific files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.