php automatically create class constants for all public properties

Viewed 83

I use laravel for my application and it often requires code with hardcoded column or property names. e.g.:

$users->groupBy('company_id');

I therefor now add class constants to all of my models:

class User {
    const COMPANY_ID = 'company_id';
}

This way I get autocompletion and it makes refactoring much easier:

$users->groupBy(User::COMPANY_ID);

I want to use the same for all my classes, but I was wondering if there is some better way than defining everything twice like this:

class Car {
    const COLOR = 'color';
    const BRAND = 'brand';
    const COUNTRY_CODE = 'countryCode';
    
    public string $color;
    public string $brand;
    public string $countryCode;
    ...
}
0 Answers
Related