php - Is it possible to skip calling __set from class internal code -


I wrote __set to make sure something done before something setter

 < Code> public function __setter ($ name, $ value) {if ($ this-> requires, nothing is needed) {$ this- & gt; DoSomeThingNecessaryBeforeSetAnything ();} $ this- & gt; Need = wrong; $ This- & gt; $ Name = $ value; }  

However, the magic method influences performance. In the same class, I have another function

  private function loadData () {if ($ this-> requires) is nothing {$ this- & gt; DoSomeThingNecessaryBeforeSetAnything ();} foreach ($ data $ key = & gt; $ value) {$ this- & gt; $ Key = $ value; }}  

Since doSomeThingNecessaryBeforeSetAnything () is already called, I do not have to call __set, but she wants to set the property directly. This will help on mass performance.
However, I can not remove __set, because there are many legacy codes outside the classroom, and I need to make the argument correct.

Feel with PHP. Any ideas on the fly can not add or remove objects?

Edit: The cause of the display is __set only, because I have a large number of objects and each has a large number of properties set I. The code given below shows that the properties of the __set set 6x faster than in comparison.

  class test {} class test 2 {public function __set ($ name, $ value) {$ this- & gt; $ Name = $ value; }} Function runon object ($ o) {$ t = microtom (true); {$ Prop = "prop {$ i}" for ($ I = 0; $ i & lt; 100000; $ i ++); $ O- & gt; $ Prop = $ i; } Echo "" (microgram (true) - $ t). "second"; } Echo RunOn Object (New Test ()) "(with outside __set) 

Echo runone object (new Test 2 ()) "(with __set)";

Result: 0.084139823913574 Second (with __set) 0.47258400917053 Second (with __set)

If you add a __get, you can store properties in a personal data structure (for example an array), which allows access to data directly from the class, while Still maintains the same public interface.

Like this:

  class containers {Private $ property = array (); Public function __set ($ key, $ value) {$ this- & gt; DoSomethingExensive (); $ This- & gt; Assets [$ of] = $ value; } Public Function __get ($ key) {If (isset ($ this-> Properties [$ key])) New exception ('Invalid property'. $ Key); } $ This- & gt; Return properties [$ of]; } Public Function Load Data ($ data) {$ this- & gt; DoSomethingExensive (); Forex currency ($ data as $ key = & gt; $ value) {$ this- & gt; Properties [$ Key] = $ value; }} Private function doSomethingExpensive () {echo 'working ...' PHP_EOL; }} Example Example $ c = New Container (); $ C- & gt; LoadData (array ('alpha' = & gt; 'A', 'beta' =>, 'd', 'charley' => gt; 'c', 'delta' = 'gt;' d ')); Var_dump ($ c-> alpha, $ c-> beta);  

If this one gets faster, I do not know, it depends on the case of your specific use because you want to avoid running "expensive" code repeatedly, but its use __get will be somewhat elevated.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -