php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #65215
Patch simplexml_countable.patch revision 2013-07-07 05:24 UTC by ww dot galen at gmail dot com
revision 2013-07-07 05:20 UTC by ww dot galen at gmail dot com

Patch simplexml_countable.patch for SimpleXML related Bug #65215

Patch version 2013-07-07 05:24 UTC

Return to Bug #65215 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: ww.galen@gmail.com

diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c
index e7c2f29..1238ad0 100644
--- a/ext/simplexml/simplexml.c
+++ b/ext/simplexml/simplexml.c
@@ -35,6 +35,9 @@
 #include "zend_exceptions.h"
 #include "zend_interfaces.h"
 #include "sxe.h"
+#if defined(HAVE_SPL)
+#  include "ext/spl/spl_iterators.h"
+#endif
 
 #define SXE_ELEMENT_BY_NAME 0
 
@@ -2622,6 +2625,9 @@ PHP_MINIT_FUNCTION(simplexml)
 	sxe_class_entry->get_iterator = php_sxe_get_iterator;
 	sxe_class_entry->iterator_funcs.funcs = &php_sxe_iterator_funcs;
 	zend_class_implements(sxe_class_entry TSRMLS_CC, 1, zend_ce_traversable);
+#if defined(HAVE_SPL)
+	zend_class_implements(sxe_class_entry TSRMLS_CC, 1, spl_ce_Countable);
+#endif
 	sxe_object_handlers.get_method = zend_get_std_object_handlers()->get_method;
 	sxe_object_handlers.get_constructor = zend_get_std_object_handlers()->get_constructor;
 	sxe_object_handlers.get_class_entry = zend_get_std_object_handlers()->get_class_entry;
diff --git a/ext/simplexml/tests/countable.phpt b/ext/simplexml/tests/countable.phpt
new file mode 100644
index 0000000..7df718a
--- /dev/null
+++ b/ext/simplexml/tests/countable.phpt
@@ -0,0 +1,28 @@
+--TEST--
+SimpleXML: Countable
+--SKIPIF--
+<?php
+if (!extension_loaded("simplexml")) print "skip";
+if (!extension_loaded('SPL')) print "skip SPL not present";
+?>
+--FILE--
+<?php
+$xml =<<<EOF
+<?xml version='1.0'?>
+<!DOCTYPE sxe SYSTEM "notfound.dtd">
+<a>
+  <b/>
+  <b/>
+</a>
+EOF;
+
+$sxe = new SimpleXMLElement($xml);
+var_dump(
+  $sxe instanceof Countable,
+  count($sxe)
+);
+===DONE===
+--EXPECTF--
+bool(true)
+int(2)
+===DONE===
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 18:01:29 2024 UTC