-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Implement a shorter way of defining empty classes #17267
Comments
Note that a syntax like |
@nielsdos Can you provide a code example where I don't know if I will have time to propose or implement an RFC, so I'm leaving this here, if someone wants to do it formally. |
It's not possible right now, but there has been discussion about this on the mailing list and on pull requests, in particular in combination with the match expression. |
@nielsdos Alright, well, we can use another syntax, as long as it's short, but I must say that the best syntax would be |
Can you clarify this use case? Why do you not use arrays? |
@TimWolla JSONs don't contain or represent just arrays/lists. In PHP, arrays are also dictionaries, but e.g.
Wouldn't it be great if we could do this
In this way, it would be a more readable, shorter and consistent with JSON itself. I also don't know if it's a good idea, but I dislike the fact that arrays in PHP are also associative. There should have been or should exist a separate dictionary data structure, but I'm not saying that we should now change how arrays work in PHP because that would most likely break a lot of code and existing functions. |
Would something like $o = (object)[];
$o = (object)['a' => 1]; be short enough? |
@MorganLOCode That might be shorter but I think still not short enough and I think that's even worse than using One of my points of introducing Nowadays, is there any other reason to use |
If there is no other reason to use Moreover, these are language changes that need to be discussed on the mailing list, not on the issue tracker. |
@Girgias JSONs are used a lot in web development. PHP is primarily a web development programming language. |
I would prefer to see less of |
Currently, one shorter way maybe something like this: class_alias('stdClass', '{}');
$a = new ('{}');
var_dump($a); It's prompted to
|
Description
You can define empty classes with
$a = new stdClass();
.However, I think that, not only this is a misnomer, because this
std
is supposed to meanstandard
and this is not really a standard class (or a base super class) but an empty class, but that we should have a shorter way to create these classes, in the same way that we can create arrays now with[ ]
.I don't know what the best syntax might be, but what about
$a = {};
?We should also be allowed to define classes like
$a = {"a" = 3}
(or something like this or maybe even be able to mimic JSON, so{"a": 1, "b": {"c": 3}}
), i.e. pre-populate the classes (if we already know some properties).I know that this syntax may be confused with dictionaries, but then JavaScript (for example) also uses something like this.
This syntax would also be somehow consistent with JSONs and one use case of these classes is also to deal with JSONs.
The text was updated successfully, but these errors were encountered: