Closed Inkognitoo closed 6 years ago
why?
Because you use camelCase for methods and functions so using snake_case for property and variables is good practice to visual divide them. I think.
Why do you need to divide them?
I am against adding more magic getters or setters. As it is they already violate visibility. (protected getter will be publicly accessible)... Let's not add more mess to the framework.
Instead, if you want it you can easily implement this in a behavior (which will be even slower). Or override your base classes (AR / model) to implement this.
Agree.
Hi @samdark,
I wonder if you can clarify something for me.
It says in the PSR2 guidelines that property names must be declared as camelCase. And alot of extensions written for yii2 (and I assume yii3) conform to this.
However, the use of the magical getters and setters when using db columns means in code we have a mixed use of
$this->somePropertyInCode
and $this->some_column_in_database
Is there any direction with regards to yii3 as to whether the standard will be altered so that properties have to be declared snake case?
As @Inkognitoo suggests - using camelCase for methods and snake_case for properties makes perfect sense - but the yii2 guidelines specfically says to use camelCase for property names and therfore we have a mix of convention when using AR. My OCD is going mental.
Wouldnt $this->some_property_in_code
and $this->some_column_in_database
be more consistent?
Bonus points for using OCD as an argument :-D
I'd say that database conventions should be updated too then?
Yes, they should.
I realise its not an actual property when you are calling $this->some_column_from_database
as it uses the getters and setters. But obviously my classes are starting to look a mess as I access properties and db columns in the same block of code.
I also realise its probably a wasted effort doing this on yii2 - but having something agreed and outlined as best practice for yii3 would be awesome. At present I am going against the yii2 standard and coding all my code to use snake_case properties. But then when I am using other developers modules and extensions they are using camelCase - so I am back to square one lol
I believe lavarel have a mapping feature so you can map db columns to camelCase convention (so everything conforms to PSR2, even Active Record) - which I think is not even needed and could cause confusion (as discussed in #17492)
If it could just be agreed that in yii3 the prefered way of declaring properties was to use snake_case then I think, over time, it would take care of itself?
Is there any direction with regards to yii3 as to whether the standard will be altered so that properties have to be declared snake case?
Yes. Yii 3 will, most likely, use Cycle ORM as a DBAL and it has a mapper so PSR-12 naming will be our go to standard.
Is there any direction with regards to yii3 as to whether the standard will be altered so that properties have to be declared snake case?
Yes. Yii 3 will, most likely, use Cycle ORM as a DBAL and it has a mapper so PSR-12 naming will be our go to standard.
Wonderful! My request is redundant in that case! Thanks
Just for completeness and clarification - PSR-12 does not appear to force you to use snake_case for properties. Its just an extension of PSR2 which states to declare properties in camelCase?
So will Yii3 use camelCase or snake_case as a preferred naming convention for properties (apologies if this is going slightly off topic of this issue - happy to open up a new one)
camelCase for properties. snake_case or for fields in DB. But you can personally use whatever is suitable for your case. Mapper is configurable.
OK, so for people with OCD like myself, you would just map your snake_case db fields to camelCase object properties? And then access $this->somePropertyInCode
and $this->someColumnInDb
(that is mapped to a db column called some_column_in_db)
Seems the most flexible, so that's good!
Assume GII will allow you build such mappings automatically if you checked something along the lines of "use camelCase for field properties".
It says in the PSR2 guidelines that property names must be declared as camelCase.
Where? AFAIK PSR-2 does not force any convention for properties names. PSR-1 explicitly avoids any recommendation in this matter: https://www.php-fig.org/psr/psr-1/#42-properties
It says in the PSR2 guidelines that property names must be declared as camelCase.
Where? AFAIK PSR-2 does not force any convention for properties names. PSR-1 explicitly avoids any recommendation in this matter: https://www.php-fig.org/psr/psr-1/#42-properties
Apologies, its not PSR-2 that forces this convention - its the yii2 standard that is forked from PSR2. Found here
Property names MUST be declared in camelCase
But the link to PSR1 throws up a contradiction with the yii2 fork.
Whatever naming convention is used SHOULD be applied consistently within a reasonable scope. That scope may be vendor-level, package-level, class-level, or method-level.
So I guess my request is - can yii3 consistently have the same naming convention for properties regardless of a whether is a code property or an active record property. And a mapper will do just that - so thats great.
Could it be argued that if you can access db fields using object properties, and yii3 properties have to be camelCase - then the field mapping should always be camel case? I guess the sensible thing would be to also not recommend a naming convention for properties as per PSR1 - but then its down to developer choice and that will result in the same issue - a mix based on what a vendor prefers to develop with :(
Perhaps I should just address my OCD haha
It's suggestion for Yii3
PSR is not say you what notation use for class property. I (and I think many programmer) use snake_case. But if you want create custom read only property for Yii in snake_case notation, you need create something like this:
It's not look good, I think =) I want to create only fullName() and have access to this like
And setter for same principle