Get and set a (private) property in PHP, like in C #, without using the magic method getter setter overload

Summary

Code example:

Class People { // private property. private $name; // other methods not shown for simplicity. } 

Straight ahead. Let me assume that $name is a member of the PRIVATE class (or a property, variable, field, name it as you wish). Is there a way to do this in PHP:

 $someone = new People(); $someone->name = $value; $somevar = $someone->name; 

WITHOUT using __get($name) and __set($name, $value) .


Background

I needed to check the assigned $value , so I just need a getter setter like this:

 getName(); setName($value); 

And it is NOT necessary to overload the magic getter setter method as follows:

 __get($value); __set($value, $name); 

However, I just need a getter. But this is NOT what I want. It just doesn't feel object oriented, because people from a static typed language like C ++ or C # can feel just like me.

Is there a way to get and set a private property in PHP, like in C #, without using overload with the getter setter magic method?


Update

Why not a magic method?

  • There are rumors on the Internet that the magic method is 10 times slower than the explicit set getter method, I have not tested it yet, but this is a good thing to keep in mind. (I realized that this is not so slow, only 2 times slower, see the test result below)

  • I need to stuff everything into one huge method, if I use the magic method, and then separate it into different functions for each property, as in an explicit getter setter. (This requirement could be answered by ircmaxell )

Performance Overhead Benchmarking

I am wondering how the performance overhead is between using the magic method and the explicit getter installer, so I created my own test for both methods, and I hope it can be useful to everyone who has read this.

With the magic method and method_exist:

(click here to see the code)

  • Getter costs 0.0004730224609375 seconds.
  • Setter cost 0.00014305114746094 seconds.

With an explicit getter installer:

(click here to see the code)

  • Getter costs 0.00020718574523926 seconds.
  • Setter cost 7.9870223999023E-5 seconds (this is 0.00007xxx).

However, both the setter and the getter with the magic method and method exist just 2x in cost than the explicit getter setter. I think it is still acceptable to use it for a small to medium system.

+5
source share
4 answers

Nope.

However, what is wrong with using __get and __set , which act as dynamic proxies with getName() and setName($val) respectively? Sort of:

 public function __get($name) { if (method_exists($this, 'get'.$name)) { $method = 'get' . $name; return $this->$method(); } else { throw new OutOfBoundsException('Member is not gettable'); } } 

This way you are not stuffing everything into one monster method, but you can still use the syntax $foo->bar = 'baz'; with private / protected member variables ...

+7
source
 ReflectionClass is your salvation 

I know this is too late for Hendra, but I'm sure it will be useful for many others.

There is a class in the core of PHP called ReflectionClass, which can manipulate everything in the scope of an object, including the visibility of properties and methods.

In my opinion, this is one of the best classes in PHP.

Let me show you an example:

If you have a private property and you want to change it from the outside

 $reflection = new ReflectionClass($objectOfYourClass); $prop = $reflection->getProperty("PrivatePropertyName"); $prop->setAccessible(true); $prop->setValue($objectOfYourClass, "SOME VALUE"); $varFoo = $prop->getValue(); 

You can do the same with eighter methods;

I hope I can help;

+3
source

If the use of magical properties seems incorrect, as already noted by other authors, you can also consider ReflectionClass :: getProperty and ReflectionProperty :: setAccessible .

Or implement the necessary methods of obtaining and installing on the class itself.

In response to the question about language features that you raised, I would say that using a language with dynamic typing is different from a language with typed statistical information. Each programming language with OOP implements it in its own way: object-oriented languages: comparison .

+2
source
 class conf_server { private $m_servidor="localhost"; private $m_usuario = "luis"; private $m_contrasena = "luis"; private $m_basededatos = "database"; public function getServer(){ return $this->m_servidor; } public function setServer($server){ $this->m_servidor=$server; } public function getUsuario(){ return $this->m_usuario; } public function setUsuario($user){ $this->m_usuario=$user; } public function getContrasena(){ return $this->m_contrasena; } public function setContrasena($password){ $this->m_contrasena=$password; } public function getBaseDatos(){ return $this->m_basededatos; } public function setBaseDatos($database){ $this->m_basededatos->$database; } } 
0
source

Source: https://habr.com/ru/post/1338758/


All Articles