Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any valid use case for using public variables in PHP OOP?

Variable encapsulation, Set/Get methods are best practices but why do we have a chance to declare a variable public if it's not meant to be used anyway? Would it have been better if variables were always private by default with no chance of making them public since all of the tutorials I read says they should be encapsulated with set/get methods? Is there any valid use case for public variables at least in PHP OOP?

like image 284
IMB Avatar asked Apr 20 '12 11:04

IMB


People also ask

What is the use of public in PHP?

public - the property or method can be accessed from everywhere. This is default. protected - the property or method can be accessed within the class and by classes derived from that class. private - the property or method can ONLY be accessed within the class.

How can use global variable inside function in PHP?

Accessing global variable inside function: The ways to access the global variable inside functions are: Using global keyword. Using array GLOBALS[var_name]: It stores all global variables in an array called $GLOBALS[var_name]. Var_name is the name of the variable.

Are PHP variables global?

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

Which is the right way of declaring a variable in PHP?

A variable starts with the $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )


2 Answers

In fact it's just the other way round: Theoretically getters/setters are wrong. The properties defines the state of an object, where the methods defines the behaviour. Getters/Setters only intercept the read and write access to properties, but they break the semantic meaning completely: Now reading the status of an object is a behaviour of the object.

To make properties to look like properties again there is a RFC on the road :) https://wiki.php.net/rfc/propertygetsetsyntax

like image 52
KingCrunch Avatar answered Oct 13 '22 20:10

KingCrunch


Set/Get methods are best practices but why do we have a chance to declare a variable public if it's not meant to be used anyway?

Best practices and not meant to be used is not the same. A language needs to offer different tools for different use-cases and should be consistent.

PHP objects always supported public members and when differentiated visibility was introduced, for backwards compatible reasons public members are very useful.

Would it have been better if variables were always private by default with no chance of making them public since all of the tutorials I read says they should be encapsulated with set/get methods?

That question can not be specifically answered, it's too subjective and there are too many different use-cases that would result in a different answers.

Is there any valid use case for public variables at least in PHP OOP?

Start with backwards compatiblity. If you can not refactor your code but would need to rewrite it completely all the time, this would be very expensive.

like image 20
hakre Avatar answered Oct 13 '22 19:10

hakre