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

Implement a shorter way of defining empty classes #17267

Open
nbro opened this issue Dec 25, 2024 · 12 comments
Open

Implement a shorter way of defining empty classes #17267

nbro opened this issue Dec 25, 2024 · 12 comments

Comments

@nbro
Copy link

nbro commented Dec 25, 2024

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 mean standard 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.

@nielsdos
Copy link
Member

Note that a syntax like $a = {}; would make block expressions impossible.
Anyway, this is a language change with implications for the syntax, so this needs an RFC.

@nbro
Copy link
Author

nbro commented Dec 25, 2024

@nielsdos Can you provide a code example where $a = {}; currently means something? If I run a program with this specific code, I get an error. This is currently not possible with PHP 8.4.2.

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.

@nielsdos
Copy link
Member

Can you provide a code example where $a = {}; currently means something? If I run a program with this specific code, I get an error. This is currently not possible with PHP 8.4.2.

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.
My point is that if we introduce this syntax for creating objects using a short notation, we exclude the ability to implement expression block.

@nbro
Copy link
Author

nbro commented Dec 25, 2024

@nielsdos Alright, well, we can use another syntax, as long as it's short, but I must say that the best syntax would be {}, because it would also be consistent with other languages and JSON.

@TimWolla
Copy link
Member

one use case of these classes is also to deal with JSONs.

Can you clarify this use case? Why do you not use arrays?

@nbro
Copy link
Author

nbro commented Dec 26, 2024

@TimWolla JSONs don't contain or represent just arrays/lists. In PHP, arrays are also dictionaries, but e.g. json_decode can return stdClass. Yes, you can tell json_decode to return an array instead, but, if we stretch this to the extreme, we could also ask why do we even have stdClass or classes in general, if we could just use arrays everywhere?

<?php

$j = json_decode('{"a": 1}');
echo "Type of: " . gettype($j) . "\n"; // object (not array)
echo $j instanceof stdClass; // true
?>

Wouldn't it be great if we could do this

<?php

$o = {"a": 1}; // an stdClass/dictionary that represents a JSON
// Maybe also dynamically populate this object/dictionary
json_encode($o); // write to file whatever
?>

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.

@MorganLOCode
Copy link

Would something like

$o = (object)[];

$o = (object)['a' => 1];

be short enough?

@nbro
Copy link
Author

nbro commented Dec 27, 2024

@MorganLOCode That might be shorter but I think still not short enough and I think that's even worse than using stdClass because we're performing a cast.

One of my points of introducing {} to define stdClasses is that we would also be able to create objects that look more like JSONs in the code.

Nowadays, is there any other reason to use stdClass other than when you're dealing with JSONs? In my experience at least, I've not used stdClass other than to deal with JSONs.

@Girgias
Copy link
Member

Girgias commented Dec 27, 2024

If there is no other reason to use stdClass outside of JSON, why should it get special treatment?

Moreover, these are language changes that need to be discussed on the mailing list, not on the issue tracker.

@nbro
Copy link
Author

nbro commented Dec 27, 2024

@Girgias JSONs are used a lot in web development. PHP is primarily a web development programming language.

@kamil-tekiela
Copy link
Member

I would prefer to see less of stdClass, maybe even remove it from the language altogether.

@TheNorthMemory
Copy link

Currently, one shorter way maybe something like this:

class_alias('stdClass', '{}');

$a = new ('{}');

var_dump($a);

It's prompted to

object(stdClass)#2 (0) {
}

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

7 participants