所向披靡是什么意思| 何其是什么意思| 为什么会呕吐| skll什么牌子| mido手表什么档次| 马上风是什么意思| 甲状腺饱满是什么意思| 辛辣的辛指什么| 白兰地属于什么酒| 特勤是干什么的| 榴莲为什么是苦的| 小便分叉是什么症状| 围魏救赵是什么意思| 幽门杆菌吃什么药最好| 小便尿起泡是什么原因| 医院测视力挂什么科| 检查是否怀孕挂什么科| 宫腔镜是什么| 梦见大领导有什么预兆| 老掉头发是什么原因| 红色的海鱼是什么鱼| 女朋友生日送什么礼物好| 做梦梦到掉牙齿是什么意思| 鱼用什么游泳| 什么情况下要打破伤风针| 炖牛肉什么时候放盐| 氯化钠敷脸有什么作用| 83年猪是什么命| 三乙醇胺是什么东西| 嬷嬷什么意思| 肾结石术后吃什么食物最好| 孕妇d2聚体高是什么原因| 飞蚊症是什么引起的| 读警校需要什么条件| 梦见妹妹是什么意思| 孕妇吃什么容易滑胎| 尿多是什么回事| 后脑勺发热是什么原因| 沙肝是什么| 臆想症是什么| 例假可以吃什么水果| 汉族人是什么人种| 相亲第一次见面送什么礼物好| 1975年属什么| 边缘是什么意思| 滑膜疝是一种什么病| 清心寡欲什么意思| 男性硬下疳是什么样子| 口水多是什么原因| 缺钙吃什么补钙最快| 3月30号是什么星座| 海带和什么不能一起吃| 肛周湿疹用什么药膏效果好| 陈皮配什么喝去湿气| 为什么会长口腔溃疡的原因| 拉仇恨是什么意思| 惊厥是什么病| 元宵节送什么| 恳谈会是什么意思| 安徽的特产是什么| 受精卵着床的时候会有什么症状| 泡汤是什么意思| 什么国家的钱最值钱| 结婚20年是什么婚| 小狗发抖是什么原因| 尾椎骨痛挂什么科| 吃柿子有什么好处和坏处| 1015是什么星座| 26度穿什么衣服| 呆若木鸡的意思是什么| 咖色搭配什么颜色好看| 8月15是什么星座| 刘字是什么偏旁| 暗物质和暗能量是什么| 牛跟什么生肖相冲| 百什么争鸣| 吃什么升血压| 咳白色泡沫痰吃什么药| 夫妻肺片是什么肉| 扶他林是什么药| 乳头痛是什么征兆| 胎儿双顶径是什么意思| 香菜什么时候种最合适| 孕妇梦见西瓜是什么意思| 周遭是什么意思| 脚趾第二个比第一个长有什么说法| 什么原因造成痫性发作| 什么的心情| 为什么眼泪是咸的| 怀孕前三个月应该注意什么| 中旬是什么意思| 独角仙长什么样| 梦见自己鼻子流血是什么预兆| 好不热闹是什么意思| 武松打的是什么虎| 头皮屑多用什么洗发水效果好| 高密度脂蛋白胆固醇偏高什么意思| 十一月九号是什么星座| 鸡眼膏为什么越贴越疼| 腿麻木是什么原因引起的| nsaid是什么药| 背后长痘是什么原因| 碘伏用什么可以洗掉| 粉色史迪仔叫什么| 红花油和活络油有什么区别| 右侧卵巢多囊样改变是什么意思| 手臂内侧是什么经络| 心衰的症状是什么| 扁桃体发炎是什么原因| b族维生素是什么意思| 降甘油三酯吃什么食物最好| 女人五行缺水是什么命| 寻常疣用什么药膏| 女性得了性病有什么症状| parzin眼镜是什么牌子| 一什么种子| 专台号是什么意思| 黑海为什么叫黑海| 满字是什么结构| 什么叫子宫腺肌症| 双子男和什么星座最配| 梦见把狗打死了是什么意思| sle是什么病| fe是什么元素| 靳东妹妹叫什么名字| cra是什么| 荷兰猪吃什么| 开窍是什么意思| 荔枝对身体有什么好处| 减肥有什么方法| 右膝关节退行性变是什么意思| 什么是全脂牛奶| gy是什么意思| 老流鼻血是什么原因| 梦见抱小女孩是什么意思| 淡然自若的意思是什么| 命中注定是什么意思| 头麻是什么原因| 牛油果坏了是什么样| 绯闻是什么意思| 子宫内膜粘连有什么症状| 孕酮起什么作用| 六味地黄丸什么时候吃| 形态各异是什么意思| 突然血糖高是什么原因引起的| kgs是什么单位| 玟字五行属什么| 口蜜什么剑| 什么的蜡烛| 移民瑞士需要什么条件| 小肠气有什么症状| 端午节都吃什么菜好| 胃病挂什么科| 32属什么生肖| 血稠吃什么药好| 一望无际是什么意思| 肌酐指标高说明什么| 上海居住证积分有什么用| 同等学力是什么意思| 黄芪不能和什么一起吃| 什么是集成灶| 百合有什么作用与功效| 经常低血糖是什么原因| 反流性食管炎挂什么科| 冬阴功汤是什么味道| 梦见爬山是什么预兆| 血糖高喝什么茶好| 微信头像用什么好| 四史指的是什么| 海马体是什么| 为什么会拉血| 什么护肤产品补水最好| 铅笔为什么叫铅笔| 12月10日什么星座| 自信过头叫什么| 焦虑症吃什么药好| 什么运动降血糖最快| 阿尔兹海默症吃什么药| 抗甲状腺球蛋白抗体高是什么意思| 四肢发达是什么生肖| 脾不统血吃什么中成药| 卖剑买牛是什么动物| 3月16号是什么星座的| 停胎是什么原因造成的| 吃什么能提高血压| 梦见牙齿掉了是什么意思| 隐翅虫皮炎用什么药膏| 出淤泥而不染是什么花| 肌红蛋白偏低什么原因| 睁一只眼闭一只眼是什么意思| 半夜口干舌燥是什么原因| 凌晨四点醒是什么原因| 桑葚泡水喝有什么功效| 口腔出血是什么病征兆| 牛初乳是什么| 清道夫吃什么| 鸡胸是什么原因引起的| 后脑勺疼痛什么原因引起的| 避孕套和安全套有什么区别| 梦见下大雪是什么预兆| 许久是什么意思| 酒店五行属什么| 救人一命胜造七级浮屠是什么意思| 霸屏是什么意思| 脉涩是什么意思| nt和无创有什么区别| 穷的生肖指什么生肖| 听诊器能听出什么| 高血压需要注意什么| 越什么越什么| 尿酸高什么不能吃| 什么油好| nuxe是什么牌子护肤品| 48年属什么生肖| 口牙是什么意思| 婚检有什么项目| hdr是什么拍照功能| 电脑一体机什么牌子好| 什么什么千山| 小鸡喜欢吃什么食物| 夜未央什么意思| 大便不成形吃什么食物好| 手表五行属什么| 男士私处瘙痒用什么药| 南什么北什么的成语| 呼吸有异味是什么原因| b2b是什么| sp什么意思| 齁甜是什么意思| 三轮体空是什么意思| 什么的叹气| 肺囊肿是什么病严重吗| g什么意思| balco是什么牌子手表| 生育保险是什么| 肝内多发低密度灶是什么意思| 送百合花代表什么意思| 人心果什么时候成熟| 皮卡丘什么意思| 环磷酰胺是什么药| 感冒输液用什么药| 小肚子是什么部位| 幼小衔接是什么意思| 丙肝有什么症状表现| 异的偏旁是什么| 3月20日什么星座| 芥蒂是什么意思| 明天代表什么生肖| 暖寿是什么意思| 大暑是什么意思啊| 罗汉果有什么功效和作用| 图谱是什么意思| 心脏是由什么组织构成的| 下焦湿热阴囊潮湿吃什么药| 青核桃皮的功效与作用是什么| 夫妻少配无刑是什么意思| 粳米是什么米| 左小腹疼是什么原因| zero是什么牌子| 前列腺钙化有什么症状| 血液病是什么| 万箭穿心是什么意思| 脸上不停的长痘痘是什么原因| 小孩子睡觉磨牙是什么原因| 百度
rfc:xmlreader_writer_streams

云南发现一处人类早期大型洞穴墓地

Introduction

百度 只有当我翻开那些旧照片,就像打开一个个贮存着记忆的保险箱,我才清清楚楚明明白白地意识到,在群体像当中,那个瘦弱不堪、矮小粗糙,那个毫不出奇的年轻人才是我老汉。

The XMLReader and XMLWriter classes deal with XML in a stream-oriented manner. The former implements an XML “pull parser”. This means that instead of keeping the data in memory or building a document tree, the document is streamed and the developer can instruct XMLReader to parse chunks at the current cursor and either process or skip the data. The advantage is that developers can process and filter large documents while requiring few resources. It is most often used as a lower-level building block for more complex handling of large XML documents. Similarly, XMLWriter writes an XML document to a stream or memory by using functions like startElement and writeElement.

There is however a strange limitation to these classes: they cannot operate on an already-open stream! This is bizarre as the APIs (both internally and user-facing) are stream-oriented. Streams that are already open are common when working, for example, with HTTP requests, data passed from a framework, or just XML data embedded in an existing stream. The lack of an API that works with already-opened streams causes developers to rely on workarounds, e.g. reading the stream entirely to memory and then using the XMLReader APIs, or writing an XML file using XMLWriter and then having to pass that into an already-open stream. That's just wasteful and needlessly difficult. This RFC aims to fix that problem and aims to fix some other inconsistency as well.

Proposal

Main Proposal

I propose to add 2 new functions, one to XMLReader and one to XMLWriter, to create an instance from a stream. Here is how they would look like:

class XMLReader {
    /** @param resource $stream */
    public static function fromStream($stream, ?string $encoding = null, int $flags = 0, ?string $documentUri = null): static {}
}
 
class XMLWriter {
    /** @param resource $stream */
    public static function toStream($stream): static {}
}

The signatures are heavily inspired by the existing function public static XMLReader::open(string $uri, ?string $encoding = null, int $flags = 0): bool|XMLReader that operate on files. However, a major difference is that XMLReader::fromStream() is static-only, whereas the other open functions of XMLReader can either be statically or non-statically called and change their return-value behaviour depending on that. The disadvantage of the existing static methods is that they can only return an instance of XMLReader, therefore when XMLReader is inherited by a user subclass we run into the problem that it doesn't return an object of the right type. We solve this by choosing static as return-type, and letting the method internally call the constructor of the static type (with no arguments). As we seem to move away from overloaded functions, I decided to only make a static method variation available.

The $documentUri parameter is used mostly for when libxml outputs error messages, such that you can put an origin name in there.

The signature for XMLWriter::toStream() should be self-explanatory. It is also modeled like the other open functions, but it is considerably simpler. You'll also notice the lack of an encoding argument, and that's because this is already handled by the XMLWriter::startDocument() function.

While implementing this, I found some strange behaviour regarding the ?string $encoding parameter of the existing functions XMLReader::open() and XMLReader::XML(). The first oddity is that they emit a warning instead of throwing a ValueError when the encoding contains NULL bytes. This is inconsistent with how other functions handle it. I propose to promote this warning to a ValueError instead. The second oddity is that invalid encoding names are ignored entirely. This means that it won't emit a warning or anything, but just silently not set the encoding. This can hide bugs. I propose to also throw a ValueError in this case stating “Argument #X ($encoding) must be a valid character encoding”.

An earlier version of this RFC proposed adding openStream() methods to both classes, but the naming was not ideal and the behaviour of being an instance method was not liked. Therefore, this was changed to static-only methods that return an instance of the respective class.

Consistency

We're adding new static named constructors to the XMLWriter and XMLReader classes. However, XMLWriter doesn't have static constructors yet and XMLReader has this hybrid static methods/instance methods we talked about earlier. Those existing methods also can't be used in subclasses because they return XMLWriter or XMLReader instead of static.

The idea is to add the following static named constructors for consistency with the newly proposed methods, with the same arguments as their existing counterpart:

  1. XMLReader::fromUrl(string $url, ?string $encoding = null, int $flags = 0): static as a new version of XMLReader::open(...)
  2. XMLReader::fromString(string $source, ?string $encoding = null, int $flags = 0): static as a new version of XMLReader::XML(...)
  3. XMLWriter::toMemory(): static as a new version of XMLWriter::openMemory(...)
  4. XMLWriter::toUrl(string $url): static as a new version of XMLWriter::openUri(...)

Note I used Url here instead of Uri because that's the more accurate term: it actually locates the resource instead of just identifying it.

This does not aim to deprecate any existing methods. We will merely update the documentation to point users towards the new constructors.

Backward Incompatible Changes

There are three minor BC breaks.

The first one is the fact that we're adding new methods. If a user extends the XMLReader or XMLWriter class, and their class implements a method with the same name but an incompatible signature, a compile error will occur. I analyzed the top 2500 Composer packages, and none used any of the proposed function names in subclasses of the XML classes. This means that the top 2500 packages don't suffer a BC break because of this. That doesn't mean there will be none, but it gives a good indication.

The second BC break is caused by throwing a ValueError on invalid encodings instead of silently ignoring invalid encodings. If we don't signal the invalid encoding in any way to the user, this can subtly hide bugs. For example, this could hide typos or silently pass invalid user input to the respective functions. Forcing developers to handle this error explicitly will result in more robust code in the end.

The third BC break is the promotion of the NUL-byte warning to a ValueError. This makes the XMLReader and XMLWriter class more consistent with other extensions that throw instead of issuing a warning. The migration for developers should be quite simple: instead of silencing the warning and/or checking the return value of the function, they should use a try-catch construct to handle the error.

Example usages

Minimal XMLReader example

// Could be any stream, but this is for simplicity sake
$h = fopen("php://memory", "w+");
fwrite($h, "<root><!--my comment--><child/></root>");
fseek($h, 0);
 
$reader = XMLReader::fromStream($h);
 
while ($reader->read()) {
    switch ($reader->nodeType) {
        case XMLReader::ELEMENT:
            echo "Element: ", $reader->name, "\n";
            break;
        case XMLReader::COMMENT:
            echo "Comment: ", $reader->value, "\n";
            break;
    }
}

Minimal XMLWriter example

// Could be any stream, but this is for simplicity sake
$h = fopen("php://output", "w");
 
$writer = XMLWriter::toStream($h);
 
$writer->startElement("root");
$writer->writeAttribute("align", "left");
$writer->writeComment("hello");
$writer->endElement();
$amount = $writer->flush();
echo "\nAmount of bytes written: ";
var_dump($amount);

Proposed PHP Version(s)

Next PHP 8.x, this is PHP 8.4 at the time of writing.

RFC Impact

To Existing Extensions

Only ext/xmlreader and ext/xmlwriter are affected.

Open Issues

None yet.

Unaffected PHP Functionality

Everything else, why do we have this section?

Future Scope

None yet.

Proposed Voting Choices

Two primary votes each requiring 2/3rd majority: one for the main proposal and one for the consistency proposal.

Voting started on 2025-08-05 and will end on 2025-08-05.

Accept adding the methods from the Main Proposal section?
Real name Yes No
adiel (adiel)  
ashnazg (ashnazg)  
crell (crell)  
derick (derick)  
girgias (girgias)  
jimw (jimw)  
kguest (kguest)  
mbeccati (mbeccati)  
nielsdos (nielsdos)  
ocramius (ocramius)  
petk (petk)  
theodorejb (theodorejb)  
Final result: 12 0
This poll has been closed.

Accept adding the methods from the Consistency section?
Real name Yes No
adiel (adiel)  
ashnazg (ashnazg)  
crell (crell)  
derick (derick)  
galvao (galvao)  
girgias (girgias)  
jimw (jimw)  
kguest (kguest)  
mbeccati (mbeccati)  
nielsdos (nielsdos)  
ocramius (ocramius)  
petk (petk)  
theodorejb (theodorejb)  
Final result: 13 0
This poll has been closed.

Patches and Tests

Implementation

References

Rejected Features

None yet.

Changelog

  1. 0.11.0: Incorporate feedback about static methods
  2. 0.10.1: Language fixes
  3. 0.10.0: Static again
  4. 0.9.2: Add example usages of the new APIs.
  5. 0.9.1: Made XMLReader::openStream() non-static instead such that it works with overridden classes.
  6. 0.9: Initial version under discussion
rfc/xmlreader_writer_streams.txt · Last modified: by 127.0.0.1

?
突然勃不起来是什么原因造成的 睡觉手发麻是什么原因 涉嫌是什么意思 什么叫电解质 移动迷宫到底讲的什么
什么叫浮小麦 减肥能吃什么水果 上午九点多是什么时辰 农历十月初八是什么星座 gmail是什么邮箱
虫草什么时间吃最好 b族维生素什么时候吃最好 直肠肿瘤不能吃什么 竹棉和纯棉有什么区别 龙须菜是什么
吃什么最补血 讹人是什么意思 哪吒的妈妈叫什么 包是什么意思 果胶是什么东西
amount是什么意思hcv7jop6ns4r.cn 今天天气适合穿什么衣服hcv9jop5ns3r.cn 咽喉发炎吃什么药hcv7jop9ns8r.cn 牛蛙吃什么inbungee.com 做梦梦到牙齿掉了是什么意思hcv8jop3ns8r.cn
什么是非萎缩性胃炎hcv8jop7ns1r.cn 什么情况不能献血0297y7.com 缺锌吃什么食物hkuteam.com 活检和穿刺有什么区别dayuxmw.com 什么是体位性低血压hcv8jop2ns3r.cn
什么是胎记hcv8jop6ns1r.cn gm眼镜是什么牌子hcv9jop2ns7r.cn 血管瘤是什么病严重吗hcv8jop6ns9r.cn 9月27日是什么星座hcv9jop0ns1r.cn 荨麻疹打什么针liaochangning.com
小什么hcv8jop0ns5r.cn 福祸相依什么意思hcv9jop3ns5r.cn 对付是什么意思hcv7jop6ns8r.cn 4像什么hcv9jop6ns9r.cn 焉是什么意思hcv7jop4ns7r.cn
百度