Hi all,

Concerning: https://bugs.php.net/bug.php?id=53727

I had put together a quick patch against PHP_5_3 to fix, for what I and others might consider, an issue with is_subclass_of() returning false in this situation:

  interface A {}
  class B implements A {}
  var_dump(is_subclass_of('B', 'A')); // false, should be true

It returns false in situations where a class does not have a parent. If a parent currently exists, the above works fine. For example:

  interface A {}
  class B implements A {}
  class C extends B {}
  var_dump(is_subclass_of('B', 'A')); // true

Attached is a patch against PHP_5_3, I can create a patch against PHP_5_4 if need be.

I've added a test file and all the current Zend/tests run as expected.

Thanks,
Ralph
diff --git a/Zend/tests/is_subclass_of.phpt b/Zend/tests/is_subclass_of.phpt
new file mode 100755
index 0000000..325e5e0
--- /dev/null
+++ b/Zend/tests/is_subclass_of.phpt
@@ -0,0 +1,20 @@
+--TEST--
+is_subclass_of() with class names for target and test should work with interfaces
+--INI--
+error_reporting=14335
+--FILE--
+<?php
+interface A {}
+class B implements A {}
+class C extends B {}
+
+var_dump(is_subclass_of('B', 'B'));
+var_dump(is_subclass_of('B', 'A')); // @see https://bugs.php.net/bug.php?id=53727
+var_dump(is_subclass_of('C', 'B'));
+var_dump(is_subclass_of('C', 'A'));
+?>
+--EXPECT--
+bool(false)
+bool(true)
+bool(true)
+bool(true)
diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c
index 6888b7f..c4031f2 100644
--- a/Zend/zend_builtin_functions.c
+++ b/Zend/zend_builtin_functions.c
@@ -844,10 +844,13 @@ static void is_a_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool only_subclass)
 		retval = 0;
 	} else {
 		if (only_subclass) {
+			if (instance_ce && instance_ce->name == (*ce)->name) {
+				// return here if the class entry names are the same
+				RETURN_FALSE;
+			}
+
 			if (!instance_ce) {
 				instance_ce = Z_OBJCE_P(obj)->parent;
-			} else {
-				instance_ce = instance_ce->parent;
 			}
 		} else {
 			instance_ce = Z_OBJCE_P(obj);

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to