K
K
Koal Koalich2018-02-28 10:09:43
Yii
Koal Koalich, 2018-02-28 10:09:43

Properties for getter and setter in PHP?

I read www.yiiframework.com/doc-2.0/guide-concept-propert...
I can't understand, but always the properties with which getters and setters work must begin with "_" ?
And if so, is this an OOP feature in php or a specific framework?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Osadchy, 2018-02-28
@waspmax1

I can be wrong, but in my opinion, in the days of php 4, private methods and properties were named with the first character _, for ease of understanding.

A
Artem Soshnikov, 2018-02-28
@artem90

Naming properties with an underscore is the wrong way, which for reasons unknown to me is followed by yii2 code-style ( https://github.com/yiisoft/yii2-coding-standards). There it is proposed to use underlining for all private variables.
the name of a variable and property should not reflect its scope, but only the assignment in the current context (method, block, closure, loop, etc.). Everything else is extra knowledge about the context, which makes the variable more "bound".

M
Maxim Fedorov, 2018-02-28
@qonand

This is a feature of Yii, related to the fact that getters and setters are called using "magic" methods when accessing an attribute

O
OnYourLips, 2018-02-28
@OnYourLips

I can’t understand, but always the properties with which getters and setters work must begin with "_"?
It's just poor quality code left over from the days of PHP4.
The PHP standards (PSR) forbid such naming.
PSR-2: 4.2. Properties
Property names SHOULD NOT be prefixed with a single underscore to indicate
protected or private visibility.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question