Skip to content
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

Feature Request: Enhanced Syntax for Property Declarations... #17029

Closed
DxBang opened this issue Dec 3, 2024 · 5 comments
Closed

Feature Request: Enhanced Syntax for Property Declarations... #17029

DxBang opened this issue Dec 3, 2024 · 5 comments

Comments

@DxBang
Copy link

DxBang commented Dec 3, 2024

I would like to request the addition of a syntax feature in PHP that allows grouping multiple properties with the same type in a class, improving code readability and reducing repetition in class definitions. This feature would be similar to the ability to declare properties in a single line with type hints introduced in PHP 7.4, but with the added flexibility to group them logically by type.

For a long time we have been able to do:

class SimplePlural {
	private
		$name = 'Plural',  // separate by commas
		$value = 0,
		$isTrue = false,
		$array = [],
		$object,
		$noType;
}

As long as we didn't give it a typed property, but since 7.4 we had to declare the visibility modifiers every time if we wanted to have typed properties on them.

class Singular {
	private string $name = 'Singular';
	private int $value = 0;
	private bool $isTrue = false;
	protected array $array = [];
	protected object $object;
	protected $noType;
}

The Proposal

Grouped Property Declarations with Type Hints

PHP currently supports declaring properties either with type hints introduced in PHP 7.4 or without type hints using a grouped style. However, a hybrid syntax combining both would simplify code readability and maintenance.

A mixture of both.

class Plural {
	private
		string $name = 'Plural',  // separate by commas
		int $value = 0,
		bool $isTrue = false;
	protected
		array $array = [],
		object $object,
		$noType;
}

A bit similar to, but without the type grouping.

class Series {
	private string
		$n = 'Se', // separate by commas
		$m = 'ri',
		$s = 'es';
	private int
		$v = 0,
		$u = 1,
		$e = 2;
	private bool
		$t = true,
		$f = false;
	private array
		$a = [],
		$r = [1, 2, 3];
	private object
		$o,
		$b;
}

Benefits:
Improved readability: Grouping related properties by type would make class definitions easier to scan, especially in larger classes.
Reduced repetition: Reduces the need to repeat the type declaration for each property, making the code more concise.
Better maintainability: It's easier to spot and manage changes for properties that share the same type.

And it would also be nice if we could create simple JSON-like objects directly instead of building them in __construct() and why not allow { } to create objects instead of (object) [ ]? It is something we are used to from JSON.

Thanks and happy coding!

@iluuu1994
Copy link
Member

Hi @DxBang. I don't understand, this is already how it works (minus the JSON thing). https://3v4l.org/Yu7nk

@DxBang
Copy link
Author

DxBang commented Dec 3, 2024

Hi @DxBang. I don't understand, this is already how it works (minus the JSON thing). https://3v4l.org/Yu7nk

Ah, yes, that one works, I just didn't read the error message correctly when I was testing it... I had 2 $a, DU'H!! I will remove part 2, thanks :)

@cmb69
Copy link
Member

cmb69 commented Dec 3, 2024

And it would also be nice if we could create simple JSON-like objects directly instead of building them in __construct() and why not allow { } to create objects instead of (object) [ ]? It is something we are used to from JSON.

See https://wiki.php.net/rfc/compact-object-property-assignment, for prior art.

I have no particular opinion on the the visibility grouping proposal.

In any way, this requires the RFC process.

@iluuu1994
Copy link
Member

@DxBang

class Plural {
	protected
		array $array = [],
		object $object,
		$noType;
}

This clashes with your other suggestion. How do we know if $noType is a continuation of $object and hence is also of type object, or new list section of properties with no types?

I'm pretty sure there have also been proposals for adding the {} syntax in the past (you may be able to find them on https://externals.io/). I don't see a big point. stdClass is not something I believe should be encouraged. You're better off using arrays, which is what stdClass uses under the hood anyway. They are also already supported in property initializers.

Copy link

github-actions bot commented Mar 4, 2025

There has not been any recent activity in this feature request. It will automatically be closed in 14 days if no further action is taken. Please see https://github.com/probot/stale#is-closing-stale-issues-really-a-good-idea to understand why we auto-close stale feature requests.

@github-actions github-actions bot added the Stale label Mar 4, 2025
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants