Why is the Directly Accesing property not recommended in PHP OOPs?

If I have a person class with the property name $ name and its getter (get_name ()) and setter (set_name ()), then after creating an instance of the objects and setting the ie property

$paddy = new person(); $paddy->set_name("Padyster Dave"); echo "Paddy full name: ".$paddy->name; //WHY THIS IS NOT RECOMMENDED... 

In the above code, $paddy->name; WHY IT IS NOT RECOMMENDED

EDIT

The above code is an example of code without assigning any attributes. Its easy to understand the concept of $ paddy-> name

+6
oop php
source share
4 answers

Since you can someday get rid of the member $name by replacing it (for example) with $first_name , $last_name and a fullname() (not that good idea ). Of course, with __get and __set , that doesn't really matter.

In general, setting a property may not be as simple as saving a value. Providing direct access to member fields can lead to other functions, as a result of which the object will be in an inconsistent state. Imagine that you are storing an array that needs to be sorted; if the array was publicly available, then another may assign an unsorted array to the field.

+6
source share

The strange thing is that you have a setter, but leave the public property. (Assuming __get magic is missing.)

Usually you want to use getters / setters to gain more control over what can be assigned to a property and what cannot. Or you may want to execute code when accessing or changing a property. In any case, you should force the use of getters / setters by creating a private or protected property, otherwise this is pretty pointless. It is about discouraging oneself or others who will use the class from firing on their own leg.

If the installer in your example only sets the value without doing anything else, it is superfluous. If it does something else that is required for each value, you have a flaw in the design of your class, since you can change the value without using setter.


 class Foo { public $bar = 0; // is only allowed to contain numeric values public function setBar($val) { if (is_numeric($val)) { $this->bar = $val; } } } $obj = new Foo(); $obj->bar = 'some value'; // set a string, which is not allowed 

If you would make $bar protected , that would be impossible.

+8
source share

Providing public fields will be a bad habit. Of a good artistic oo principle in PHP

If something changes with the object, any code that uses it must also change. For example, if a person’s first name, last name, and other names need to be encapsulated in a PersonName object, you will need to change all your code to make changes.

+3
source share

Obviously, I am doing the “bad” things, since I use this type of code all the time.

But what if $ this-> name (or how I would do $ this-> nameFormatted) is constructed like this:

 protected var $name; $this->name = $this->getSalutation() . ' ' . $this->nameFirst . ' ' . $this->nameLast; 

or something like that.

You use $ this-> name in a public context, but it is still constructed in the class, and if it is protected, it will not be overwritten.

Or am I missing a point, and the “bad” thing actually sets a name in the public domain?

0
source share

All Articles