ReflectionClass::newInstanceWithoutConstructor() - php 反射机制
ReflectionClass::newInstanceWithoutConstructor()
(PHP 5 >= 5.4.0, PHP 7)
创建一个新的类实例而不调用它的构造函数
说明
publicReflectionClass::newInstanceWithoutConstructor(void): object创建一个新的类的实例而不调用它的构造函数。
参数
返回值
更新日志
版本 | 说明 |
---|---|
5.6.0 | All internal classes can now be instantiated except for those declaredfinal. |
错误/异常
如果这个类是一个不能不调用构造函数来实例化的内置类,将导致一个ReflectionException。在 PHP 5.6.0 及更高版本中,此异常仅限于final的内置类。
参见
- ReflectionClass::newInstance() 从指定的参数创建一个新的类实例
- ReflectionClass::newInstanceArgs() 从给出的参数创建一个新的类实例。
It should be made clear that from an OOP theory perspective the use of this method is very bad practice in the same manner as goto, eval and singletons. If you find a need to use it in production code you're almost certainly doing something wrong somewhere. It may occasionally be useful for debugging, but even then hints at poor initial code. The problem? It breaks encapsulation. An object can exist in the application but may not be able to fulfill its responsibility because it's missing dependencies. The use of this method makes it possible for an incomplete object to exist in the system; the object can exist in a state that its author never intended. This is bad because it will cause unexpected things to happen! A fundamental principle in OOP is that objects are in complete control of their state, the use of this method prevents that guarantee. n.b. The annotation based "dependency injection" listed below is not a solution or valid use-case for this either because it breaks encapsulation (Among other things!) and the class being constructed needs to know of the container by providing annotations.
If this method is not available in your version of PHP you can use a trick to create an instance without calling the constructor. Use reflection to get the properties and default values of the class, and create a fake "serialized" string. Example: Output: object(foo)#6 (4) { ["a"]=> int(10) ["b":protected]=> int(2) ["c":"foo":private]=> string(7) "default" ["d":protected]=> NULL } I hope this can help someone. Oliver Anan
sorry for replying to such an old comment, but there's something i wanted to point out. @ tom at r dot je: While I agree with what you said in general, there *are* cases where it's not true, and since PHP doesn't allow for multiple constructors there's no other good way around it. > The problem? It breaks encapsulation. > An object can exist in the application but may not be able to fulfill its > responsibility because it's missing dependencies. > The use of this method makes it possible for an incomplete object > to exist in the system; > the object can exist in a state that its author never intended. If you're using this method from some factory method, to manually init the object in other way that the constructor that, this point is no longer valid. Consider the example, where you use constructor to set-up the object after getting it from database (e.g. you need to pull some array from other table, basing on some id parameter). But you want to also be able to create object manually (for inserting to the database, for example). The best thing to do would be to have two different constructors, but since it is not possible in php you need some other way to create the object. example: Of course, you can instead make empty constructor, and create some init() method, but then you have to remember to call init() everywhere. You can also create some other way of adding the item to database, but then you'd have to classes to handle the same data - one for retrieving and other for storing. If you instead use some factory class (or just a factory method, like in the simplified example above) having a way to create totally empty object is useful. With full factory approach you would first use some TestFactory->prepare() method, then you'd call methods to set what you need, and the factory would set all uninitialized variables to some default values upon calling TestFactory->get() to retrieve prepared object.
A good first use for this new method. It implements a transparent parser constructor argument to achieve 99% reusable component.
This new Feature enables Annotation based Dependency Injection:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)