带状疱疹能吃什么食物| 肚脐眼左侧是什么器官| 曲率是什么意思| 牛蛙和青蛙有什么区别| 9月3日是什么纪念日| 什么学什么问| 4月29是什么星座| 求嗣是什么意思| 后背疼是什么原因引起的| 光感是什么意思| 百日咳是什么意思| 缺少雌激素的女性会有什么症状| 什么的太阳| 火疖子吃什么药| 神经性头痛吃什么药好| 96年属鼠的是什么命| 精神心理科主要治疗什么疾病| 血栓的症状是什么| 窗户代表什么生肖| 什么中药可以减肥| 腹部胀疼是什么原因| 子宫内膜薄有什么危害| 液基薄层细胞制片术是检查什么的| 呼吸衰竭是什么意思| bebe是什么牌子| 肺结节吃什么药能散结| 洛基是什么神| 劲爆是什么意思| 1月13日什么星座| 经络是什么意思| 宫颈管搔刮术是什么| 贪心不足蛇吞象什么意思| x什么意思| 7月12日是什么星座| 金牛座后面是什么星座| 清晨醒来口苦是什么原因| 健康管理师是干什么的| 做妇科检查前需要注意什么| 呼吸困难气短是什么原因| bp是什么意思医学上面| 刀枪不入是什么生肖| 库克是什么| 什么食物含钾高| 九月八号是什么星座| doge是什么意思| 摄人心魄是什么意思| 牛肉粉是什么调料| 结膜炎用什么眼药水效果好| 梦见菊花是什么意思啊| 人体第一道防线是什么| dazzle是什么牌子| 笑点低的人说明什么| 霄是什么意思| 梦见动物是什么意思| 17楼五行属什么| 梅毒吃什么药| 放电是什么意思| 麦子什么时候收割| 口干口苦什么原因| 城是什么生肖| 扁桃是什么水果| 下焦湿热阴囊潮湿吃什么药| 膀胱切除后有什么影响| 高密度脂蛋白偏高是什么原因| 女鼠配什么属相最好| 请柬写伉俪什么意思| 什么是血浆| 迄今为止什么意思| 男女双修什么意思| 眼睛干涩是什么原因| 胃胀放屁多是什么原因| 猴子是什么动物| superstar什么意思| 套马的汉子你威武雄壮是什么歌| 同甘共苦什么意思| 普陀山求什么最灵| 馨是什么意思| 什么时候刮胡子最好| 善莫大焉什么意思| 进口二甲双胍叫什么| 左下眼皮跳是什么原因| 脑子疼是什么原因| 有缘无分是什么意思| 女左上眼皮跳是什么预兆| 生理盐水和食用盐水有什么区别| 胃不好应该吃什么| 失眠是什么原因引起的| 什么是再生纤维素纤维| 割包皮应该挂什么科| 通讯地址是什么意思| 双侧骶髂关节致密性骨炎是什么病| 左耳烫代表什么预兆| 喝醋有什么好处| 肾阴阳两虚吃什么药| 高考吃什么菜| 10月28日是什么日子| 孙悟空叫什么名字| 结晶体是什么意思| 女性漏尿吃什么药最好| 不眠之夜是什么意思| 口舌生疮是什么原因| 洋葱炒什么好吃| 莫迪是什么种姓| 梦见情敌什么预兆| 结节性甲状腺肿是什么意思| zhr是什么牌子的鞋| 胃胀呕吐是什么原因| 左眼皮跳什么预兆| 什么是超声波| 裳能组什么词| 高锰酸钾治疗男性什么病| 网恋是什么意思| 忌动土是什么意思| prep是什么药| 看门神是什么生肖| 什么是阴蒂| def是什么意思| 手臂突然疼痛什么原因| 化验血挂什么科| 荷花是什么时候开的| 乳腺增生1类什么意思| 懂事是什么意思| 7月1日什么节日| 滋阴潜阳是什么意思| 心仪什么意思| 喜爱的反义词是什么| 风湿是什么原因引起的| 食用油是什么油| 霉菌是什么原因感染的| 转的第三笔是什么| 为什么叫买东西不叫买南北| 指甲盖上有竖纹是什么原因| 突然抽搐是什么原因| 一月底是什么星座| 蒸汽机是什么| 梦见很多小蛇是什么意思| 养狗需要注意什么| 弥陀是什么意思| 更年期什么年龄开始| 145是什么意思| 崩溃什么意思| 属蛇的人适合佩戴什么| 香菇和什么不能一起吃| 1889年属什么生肖| cems是什么意思| 痔疮的初期症状是什么| 319是什么星座| 长沙有什么大学| 中华文化的精髓是什么| 陋习什么意思| 掼蛋是什么意思| 吉利丁片是什么做的| 公元400年是什么朝代| 小赤佬是什么意思| 女人骨质疏松吃什么最好| 注会什么时候考试| 夏天是什么时候| 气道高反应是什么意思| 日央读什么| 烧伤病人吃什么恢复快| 11.9是什么星座| 苯扎氯铵是什么| 屁股沟疼是什么原因| 肠痉挛是什么症状| 内伤湿滞什么意思| 吃万艾可有什么副作用| 喝酒前吃什么不容易醉| 梦见死去的亲人又活了是什么意思| 闻名的闻什么意思| 健康证都查什么| 气管炎咳嗽吃什么药最有效| 红细胞偏高是什么病| 阳虚水泛是什么症状| 口腔医学是干什么的| 检查贫血挂什么科| 中药液是什么药| 梦到自己怀孕了是什么预兆| 什么组词| 鸣是什么家禽| 才高八斗是什么生肖| 反讽是什么意思| 幽门螺旋杆菌什么症状| 梦见好多葡萄是什么意思| 处女座男和什么座最配对| rh血型阴性是什么意思| 下巴发黑是什么原因| 什么时候是情人节| 血液肿瘤是什么病| 心仪的人是什么意思| 1893年属什么| 眉茶属于什么茶| 怕冷又怕热是什么原因| 一周不排便是什么原因| 手心脚心发热吃什么药| 一什么三什么的成语| 自缚是什么意思| 兔儿爷是什么意思| 脸部痒是什么原因| 承蒙不弃什么意思| 什么油最健康| 什么人什么天| 腺癌是什么原因引起的| 0101是什么意思| 田七与三七有什么区别| kids是什么品牌| 长长的柳条像什么| 德育是什么| 早上口苦是什么原因| 妈妈的弟弟的老婆叫什么| 喉咙卡鱼刺挂什么科| 吃海鲜喝什么酒| 荔枝代表什么寓意| 生忌是什么意思| 肚子为什么胀气| 一棵树是什么品牌| 卡西欧手表什么档次| 吃什么能解决便秘| 孽缘是什么意思| 622188开头是什么银行| 来例假吃什么水果| 一九六八年属什么生肖| 孕妇吃葡萄对胎儿有什么好处| 什么叫肝功能不全| 扁桃体长什么样| 脂溢性脱发用什么洗发水好| 松字五行属什么| 耳鸣是什么病的前兆| 什么南瓜| 什么时候测试怀孕最准确的| 什么叫随机血糖| 梦到公鸡是什么意思| 1月8日是什么星座| 乐器之王是什么乐器| 记性不好吃什么药| 心衰什么症状| 属狗的是什么命| reald厅什么意思| 孕期什么时候补钙| 丝光棉是什么材质| 公粮是什么意思| 女同是什么| 测子女缘什么时候到来| 番茄酱和番茄沙司有什么区别| 粒细胞低是什么原因| 嗓子疼看什么科室| 白头翁吃什么| 嫣字五行属什么| 罗森是什么| 兆以上的计数单位是什么| 甲基硫菌灵治什么病| 坚果补充什么营养成分| 青蒿素是什么| samsung是什么牌子| hev是什么病毒| 肾衰竭吃什么水果好| 梦到借钱给别人什么意思| 经期延长是什么原因引起的| 易胖体质是什么原因造成的| 6月30号什么星座| 公检法是什么| 奔走相告的走是什么意思| 人绒毛膜促性腺激素是查什么的| 中国最早的文字是什么| opple是什么牌子| 百度
rfc:returntypehint2

湖南电力交易一季度成交85.6亿千瓦时——新华网——湖南

Introduction

百度 如果美方执意要打贸易战,中国也只能奉陪到底,绝不会坐视自身合法权益受到损害。

The purpose behind this RFC is to introduce return type hinting to PHP class methods. This RFC differs from the existing entry with a completely different concept and patch. The language changes taken by this approach are, in my opinion, more advantageous because it provides a more familiar interface to method type hinting and uses the existing standard created by parameter type hinting.

Syntactical Implementation

Approach

Languages like C# and Java provide an elegant syntactical approach to defining this:

[method_attributes] [return_type] [method_name] '(' parameters ')'

When writing this patch, I saw no reason diverge from this direction. However, there was a caveat: PHP has the pseudo-type “mixed”, which doesn't actually have a keyword. To top this off, the existing definitions for methods require the keyword “function” in order for the language parser (zend_language_parser.y) to detect the entry. Making a change to the language requiring a return type hint would break compatibility and be completely unacceptable. Rather than this creating a challenge requiring a workaround, the BC issue actually resolved the issue! Keeping the existing declaration using “function” could serve as the mixed type and maintain BC.

Using Existing PHP Standards

With the introduction to the new object model in PHP 5 came parameter type hinting. Developers could define objects, arrays (as of version 5.1) and callable (as of version 5.4), forcing the caller to provide a precise type or face raising a catchable fatal error. This eliminated the need for functions and methods to constantly contain blocks of code verifying a provided value is a specific type. Moving this check to the Zend Engine not only made execution faster, but it also saved precious development time. This approach inherently provides valuable documentation and establishes a higher level of confidence in code.

This implementation of return type hinting follows the same approach. This provides consistency that developers appreciate. When and if type hinting allows for additional types, the change can be applied to both methods and parameters.

Returning NULL

By default, if you specify a return type hint, you must return that type. However, a new keyword “nullable” has been added to bypass this requirement. Much like the parameter type hinting, this provides developers with added flexibility, and allows developers using an API to easily identify and code for that situation.

Example Implementations

<?php
 
class MyClass
{
    public ArrayIterator getIterator()
    {
        return new ArrayIterator();
    }
 
    public array getArray()
    {
        $array = array('some','array','values');
        return $array;
    }
 
    public function getMixedValue()
    {
        return 'Anything can be returned here';
    }
 
    public \ArrayIterator getNamespacedVersion()
    {
        return new ArrayIterator();
    }
 
    public callable getCallableString()
    {
        return 'strlen';
    }
 
    // The nullable keyword allows you to return null 
    protected nullable ArrayObject getArrayObject()
    {
        return null;
    }
 
    private callable methodModsDontMatter()
    {
        return 'str_replace';
    }
 
    ArrayObject methodsDontNeedModsActually()
    {
        return new ArrayObject();
    }
}

The above examples show how some of the functionality works.

Interfaces

The interface changes introduced in this patch provide compile-time validation on inheritance and implementation. Abstract methods implemented in classes will be required to adhere to the type hint defined by the interface definition. If they aren't compatible, an E_COMPILE_ERROR is raised. There is a strict guideline: if an object, array or mixed (function keyword) are defined, the implementing method will not be able to change this.

Interface names may also be declared as the type hint for return values. The following example illustrates an implementation of this:

<?php
 
interface IteratorInterface
{
    public ArrayIterator getIterator();
}
 
class Users implements IteratorInterface
{
    public ArrayIterator getIterator() 
    {
        /* do some work */
        return new ArrayIterator();
    }
}
 
class Vehicles implements IteratorInterface
{
    public ArrayIterator getIterator()
    {
        /* do some work */
        return new ArrayIterator();
    }
}
 
class MyClass
{
    public IteratorInterface getTheIterator()
    {
        return new Vehicles(); // This is valid
    }
 
    public IteratorInterface getTheOtherIterator()
    {
        return new Users(); // This is valid, too
    }
}

The above example shows two classes (Vehicles and Users) that implement an interface (IteratorInterface) and a class that provides methods defining the interface as the return type hint. Any class that implements an interface may be returned from a method that defines such.

Reflection

Reflection has a minor change in this patch. A new method called “getReturnType” has been added to ReflectionMethod which returns one of the following values: “mixed”, “array”, “callable” or “{ClassName}” (the actual class' name).

Functional Implementation

I will briefly describe the functional changes made, and let the patch do the rest.

Parser

The language parser “Zend/zend_language_parser.y” has been modified to add an additional term called “method_return_type”. The definition for this contains two tokens, T_CALLABLE, T_ARRAY, and one term - fully_qualified_class_name. The method_return_type was added to class_statement, just above “function”.

I separated the “function” and “method_return_type” definitions within class_statement for two reasons. First, I didn't want to introduce any additional changes to the compiler function zend_do_begin_function_declaration. Since all function and method declarations go through this today, I didn't want to add logic within this just to verify if a method has defined a type hint. This would've ended up in multiple places throughout the function, so I thought it would be best to let the parser deal with this once.

Second, and not a technical decision, I didn't want to introduce these changes on the first iteration. Can this functionality be combined into the zend_do_begin_function_declaration? Yes. Would doing so affect the performance of the compilation? It absolutely would. Every function and method call would require a logical check to verify if a type hint is defined.

Compiler

As previously stated, a new function “zend_do_begin_returntype_method_declaration” was added. This function takes care of verifying whether an array or object is defined, allocating and resolving the class name and finishing the additional tasks which are required for all methods and functions. This does contain some redundant code from zend_do_begin_function_declaration, but in time, this can be resolved.

I also modified zend_do_begin_function_declaration to declare the type hint as IS_UNUSED and nullify the class name.

I decided to add the type hint data elements to the zend_op_array structure rather than the zend_function union. My main goal with this is to introduce a standard location for storing this data. Today, only parameters contain type hinting. In the future, methods (this RFC) and other language elements (e.g. accessors) could contain type hinting functionality, and my hope is they will be able to take advantage of using an already defined location. I prefixed the names with “method_”, but that can easily be changed within this patch or in the future.

Executor

Three functions were added to zend_execute.c:

  1. zend_verify_method_return_type - this verifies if a method adheres to the defined return type
  2. zend_verify_method_return_error - like its parameter counterpart, this determines the correct type of error to raise
  3. zend_verify_method_return_class_kind - if it is determined an object is being returned, this function is called to resolve whether a class or interface name should be provided along with the correct error matching that.

The zend_vm_execute.h was modified in multiple places to reference zend_verify_method_return_type. If it is determined the function or method doesn't have a definition defined, it quickly continues the code execution. If a definition is provided, it verifies the type provided by method, and either allows the continuation of the execution, or raises the catchable fatal error.

Tests

A total of 21 tests were added to tests/classes/. All tests file names are prefixed with “method_returntype_” for easy identification. The tests do the following:

  • Determine that non-namespaced and namespaced class name don't produce a syntax error
  • Determines if a catchable fatal error is raised when an array or object is defined and the following are returned: resource, object, string, integer or double. There are referenced return counterparts for these as well.
  • Determine if an E_COMPILE_ERROR is raised when a class implements an interface that defines a method which returns both an array and object, but fails to correctly redefine.
  • Determines if a callable works
  • Determines if a callable is defined but not returned

Patch

The patch for this is now outdated and gone.

Changelog

  1. Updated to include “callable” as an accepted return type. Includes patch and documentation changes
  2. Updated to remove allowing NULL to be returned unconditionally when declaring return types
  3. Added a new patch which includes a “nullable” token for declaring a method may return null
  4. Updated the RFC to take “nullable” into account and replaced the old patch with the new functionality
rfc/returntypehint2.txt · Last modified: by 127.0.0.1

?
拉黑色大便是什么原因 96年属鼠的是什么命 老人大小便失禁是什么原因造成的 空调睡眠模式什么意思 杨梅吃了有什么好处
无精是什么原因造成的 suv是什么意思 什么病可以鉴定病残 桑葚什么季节成熟 什么品牌的书包质量好
氨糖是什么 左侧卵巢囊性回声是什么意思 广州地铁什么时候停运 被动是什么意思 dw手表属于什么档次
快的反义词是什么 碘131是什么 格局小是什么意思 闲暇的意思是什么 蒲公英什么时候采最好
雷字五行属什么hcv7jop4ns8r.cn 什么时候做人流才是最佳时间hlguo.com 梅子什么时候成熟hcv9jop0ns5r.cn 现在是什么季节hcv7jop6ns6r.cn 心脏痛吃什么药效果好hcv9jop6ns2r.cn
电子商务学什么qingzhougame.com 为什么会磨牙hcv8jop4ns6r.cn 巧克力囊肿有什么症状表现hcv8jop0ns5r.cn 气压治疗是什么hlguo.com 疼和痛有什么区别hcv7jop7ns2r.cn
声嘶力竭是什么意思hcv9jop5ns1r.cn yp是什么意思hcv8jop1ns9r.cn 火龙果吃了有什么好处hcv9jop4ns1r.cn 骨科是什么inbungee.com 大地色眼影是什么颜色hcv8jop7ns4r.cn
oversize是什么意思hcv7jop6ns5r.cn 急性上呼吸道感染是什么引起的1949doufunao.com aug是什么意思hcv8jop1ns2r.cn 刚出生的小鱼苗吃什么xianpinbao.com 嘴巴发麻是什么原因hcv7jop9ns1r.cn
百度