When justified the use of public fields in PHP?

0 like 0 dislike
27 views
When justified the use of public fields in PHP?
The question from the point of view of the PLO.
by | 27 views

6 Answers

0 like 0 dislike
Then when in any other language. Than php in that stands out?
\r
Then when you need to set some parameters of the object from the outside. Or the question is that instead of public fields, use public methods of the form $myObj->GetName(); $myObj->SetName("New Name"); or public __get and __set? Well, if no immediate response object, the value change is not needed, why not use fields? Simply by setting or reading a variable is faster than calling a method or tembolee using __get and __set inside which all the same assignment or reading a variable.
\r
Or do you have public fields offer to refuse? But as a object then to manage? :)
by
0 like 0 dislike
There are two schools. The first of these teaches that in the class where the variable is defined, refer to it should be free (immediate access). The other school argues that even within this class you should always use accessors (indirect access).
\r
The advantage of indirect method is that it allows you to override in a subclass a method of obtaining information and providing greater flexibility in data management, such as lazy initialization (lazy initialization).
\r
The benefit of direct access is to ease the code reading. It is not necessary to stop thinking "it's just a method of obtaining the values of the variable"
\r
Usually, we first use direct access to variables, until it becomes an obstacle. You can then move to the indirect method.
\r
Damn, where did I read it?
by
0 like 0 dislike
I use public methods to the Entities, i.e. objects that reflect the essence of the system, for example, the object "User"
\r
$user->id = 1; $user->username = 'user'; $user->firstName = 'First name'; $user->lastName = 'Last name'; 
by
0 like 0 dislike
Under the field you are a class member?
If Yes, IMHO never.
From the standpoint of future-proofing under the change of members is more profitable to define public methods. In this case, the transition from a two-dimensional coordinate system for three-dimensional't have to rewrite half the app(a sort of sample in vacuum with the public $coordinates=array(array('x'=>1,'y'=>2))).
by
0 like 0 dislike
I believe that in this matter, as in any other, no need to resort to extremes, i.e. how never use public fields, and use them everywhere — bad. In particular, it is unwise to use a class with 20 private fields, each of which has methods getX and setX.
\r
For example, in C/C++ there are structures (struct), and the use of structures built a big part of any API. And nothing bad in it do not see. In PHP there are no structures, but their role can play classes with public fields.
\r
Still, I think it makes sense to use public fields in that part of the system that is not visible to the outside, ie, where all the code is under your control. Sometimes it will make the code cleaner and more concise.
\r
Still, for example, where the object is used for one time transmission unit of the information and subsequent field change will not affect the performance of the system (say, $object event with the fields name, type, sender, message). Etc.
by
0 like 0 dislike
Classic OOP does not recommend the use of public fields. It is recommended that access can be arranged through the getters and setters (this is not PHP shnye __get & __set, and wrapper methods getName(), setName()...).
\r
It is possible to use public fields where you do not need test data that uses simple data objects. We use hashmessage[ $ob = array(); $ob[id]=1;... ] as a data structure. It is possible and even better is to use the objects: $ob = new StdClass(); $ob->id = 1; $ob->name=... we, in this case, all field work public.
\r
It is not advisable in this case to write a custom class to each field and its getter and setter if the function of this object is only storing/sending information.
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
6 answers
110,608 questions
257,186 answers
0 comments
32,865 users