关于__set()的问题
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <?php namespace longli; class Employee { private $name; public function __set($xproName, $value) { echo '<p>Employee::__set is called $proName=='.$xproName.', $value=='.$value.''; $this->xproName = $value; } } class CEO extends Employee { function __construct($name="") { echo '<h2 id="CEO-construct-begin">CEO::__construct begin!</h2>'; $this->name = $name; echo '<h2 id="CEO-construct-end">CEO::__construct end!</h2>'; } } $boss = new CEO("Blus"); ?>
上面的代码,__set()被莫名其妙执行了两次,请问这是为什么呢? 输出结果为:
CEO::__construct begin!
Employee::__set is called $proName==name, $value==Blus
Employee::__set is called $proName==xproName, $value==Blus
CEO::__construct end!
------解决方案--------------------
__set( $property, $value ) : 给一个未定义的属性赋值时调用
你赋值了两次当然就执行两次了。
------解决方案--------------------
第一次给未定义的类成员$name赋值: $this->name = $name; 调用一次
在调用重载方法__set的时候,第二次给未定义的类成员 $xproName赋值: $this->xproName = $value; 调用一次。
我想你的本意是这样:
- PHP code
public function __set($xproName, $value) { echo '<p>Employee::__set is called $proName=='.$xproName.', $value=='.$value.'</p>'; // 这个 $xproName是一个变量,内容是变量名。 详见手册中 可变变量 $this->$xproName = $value; } <br><font color="#e78608">------解决方案--------------------</font><br>预定义了两个函数“__get()”和“__set()”来获取和赋值其属性,以及检查属性的“__isset()”<br><br><br>//__get()方法用来获取私有属性<br>private function __get($property_name)<br>{<br>if(isset($this->$property_name))<br>{<br>return($this->$property_name);<br>}else<br>{<br>return(NULL);<br>}<br>}<br>//__set()方法用来设置私有属性<br>private function__set($property_name,$value)<br>{<br>$this->$property_name=$value;<br>} <br><font color="#e78608">------解决方案--------------------</font><br>你仍然没弄清楚__set()的作用,当你为类成员属性$xproName赋值后,它已经不再是未定义了。所以不具备死循环的条件。<br>我觉得用__set()来实现的封装是过分的封装,为什么要在类外部修改一个隐藏的属性?为什么不设为公有?<br><br>至于说 事实上,为了“添加一个隐藏属性”<br>添加一个隐藏的属性?你的程序需要的属性名难道不是固定的吗?你应该先声明该隐藏属性。<br> <br><font color="#e78608">------解决方案--------------------</font><br>所用面向对象的语言中都对对象的每个属性设有 Access 和 Assign 方法,以便开发者灵活的控制对象的行为。由于 php 不是面向对象的语言,所以虽然粗糙的提供了 __get 和 __set 方法,但与 Access 和 Assign 相比还是有很大差距的<br>与 Access 和 Assign 一样,如果仅仅将 __get 和 __set 作用于对象属性的存取,就大有画蛇添足的嫌疑<br> __get 和 __set 在对象中的作用是在存取属性的同时隐式的之行一些方法,而无需显式的调用方法<br> <div class="clear"> </div>

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
