Declaring protected variables in methods

I had a good look and did not seem to find an answer to this problem.

I mainly use the _call method to dynamically create the get and set methods, however when declaring a variable, PHP default is public. Is it even necessary to declare a variable from a class as protected?

function __call($method, $arguments) { $prefix = strtolower(substr($method, 0, 3)); $property = strtolower(substr($method, 3)); if (empty($prefix) || empty($property)) { return; } if ($prefix == "get" && isset($this->$property)) { return $this->$property; } if ($prefix == "set") { $this->$property = $arguments[0]; } } 
+4
source share
3 answers

One option is to have a protected array and install an element from your magic setter in this array.

 class MyClass { protected $_myProperties = array(); public function __get($name) { if (isset($this->_myProperties[$name])) { return $this->_myProperties[$name]; } else { return null; } } public function __set($name, $value) { $this->_myProperties[$name] = $value; } public function __isset($name) { return isset($this->_myProperties[$name]); } } 
+6
source

First, I would suggest NOT returning if the prefix or property variables are not set. This will make debugging VERY difficult. Instead, replace return; on throw new BadMethodCallException('Method Does Not Exist: '.$method);

Secondly, doesn't that defeat the point of protected variables? It allows you to read and write all properties without any verification. If you intend to do this, you can also make them publicly available.

I personally consider $foo->bar = 'baz'; more readable than $foo->setBar('baz'); . Not because it is β€œeasier” to understand, but because the second is too detailed.

Personally, I suggest doing __get and __set and adding validation. The whole point of protecting variables is trust (so you can trust the settings). Of course, you can use reflection or subclassification to change them, but I usually assume that if someone goes this far, they deserve any unintentional conspiracies if they ruined the variable.

And keep in mind that if you use some kind of magic method, you will need to add documentation elements if you want your ID environment to tell you methods / variables ...

EDIT:

And don't forget that if you declare __get / __set , you can override them in child classes. Therefore, if a child declares new variables, you can process them there, and then call parent::__get to handle the default variables. Therefore, do not use the array only so that you have one method for all children. Do validation for members you know about and let your children do their own validation ...

+5
source

Is it possible to declare a variable from a class as protected?

Does not look like. You can use Reflection to change the accessibility of properties , but it seems that this can only be used effectively to make things public that were not previously public.

You might want to consider storing automatically generated properties in an array with appropriate visibility.

(There are also __get and __set , but they may not suit your needs. They will only be called when the property is absent or unavailable. Classes that may relate to protected properties bypass them.)

+2
source

All Articles