- Core:
- get_declared_traits()
- http_response_code()
+ - trait_exists()
f. New global constants
--TEST--
-Checking if exists interface, abstract and final class
+Checking if exists interface, trait, abstract and final class
--FILE--
<?php
final class c { }
+trait d {}
+
var_dump(class_exists('a'));
var_dump(class_exists('b'));
var_dump(class_exists('c'));
+var_dump(class_exists('d'));
?>
--EXPECT--
bool(false)
bool(true)
bool(true)
+bool(false)
--- /dev/null
+--TEST--
+Testing trait_exists()
+--FILE--
+<?php
+
+trait foo {
+}
+
+var_dump(trait_exists('foo'));
+var_dump(trait_exists(1));
+var_dump(trait_exists(NULL));
+var_dump(trait_exists(new stdClass));
+
+?>
+--EXPECTF--
+bool(true)
+bool(false)
+bool(false)
+
+Warning: trait_exists() expects parameter 1 to be string, object given in %s on line %d
+NULL
--- /dev/null
+--TEST--
+Testing trait_exists() inside a namespace
+--FILE--
+<?php
+
+namespace foo;
+
+trait IFoo { }
+
+trait ITest { }
+
+
+var_dump(trait_exists('IFoo'));
+var_dump(trait_exists('foo\\IFoo'));
+var_dump(trait_exists('FOO\\ITEST'));
+
+?>
+--EXPECT--
+bool(false)
+bool(true)
+bool(true)
--- /dev/null
+--TEST--
+Checking trait_exists(): interface, trait, abstract and final class
+--FILE--
+<?php
+
+interface a { }
+
+abstract class b { }
+
+final class c { }
+
+trait d {}
+
+var_dump(trait_exists('a'));
+var_dump(trait_exists('b'));
+var_dump(trait_exists('c'));
+var_dump(trait_exists('d'));
+
+?>
+--EXPECT--
+bool(false)
+bool(false)
+bool(false)
+bool(true)
static ZEND_FUNCTION(property_exists);
static ZEND_FUNCTION(class_exists);
static ZEND_FUNCTION(interface_exists);
+static ZEND_FUNCTION(trait_exists);
static ZEND_FUNCTION(function_exists);
static ZEND_FUNCTION(class_alias);
#if ZEND_DEBUG
ZEND_ARG_INFO(0, autoload)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO_EX(arginfo_trait_exists, 0, 0, 1)
+ ZEND_ARG_INFO(0, traitname)
+ ZEND_ARG_INFO(0, autoload)
+ZEND_END_ARG_INFO()
+
ZEND_BEGIN_ARG_INFO_EX(arginfo_function_exists, 0, 0, 1)
ZEND_ARG_INFO(0, function_name)
ZEND_END_ARG_INFO()
ZEND_FE(property_exists, arginfo_property_exists)
ZEND_FE(class_exists, arginfo_class_exists)
ZEND_FE(interface_exists, arginfo_class_exists)
+ ZEND_FE(trait_exists, arginfo_trait_exists)
ZEND_FE(function_exists, arginfo_function_exists)
ZEND_FE(class_alias, arginfo_class_alias)
#if ZEND_DEBUG
found = zend_hash_find(EG(class_table), name, len+1, (void **) &ce);
free_alloca(lc_name, use_heap);
- RETURN_BOOL(found == SUCCESS && !((*ce)->ce_flags & ZEND_ACC_INTERFACE));
+ RETURN_BOOL(found == SUCCESS && !((*ce)->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS));
}
if (zend_lookup_class(class_name, class_name_len, &ce TSRMLS_CC) == SUCCESS) {
- RETURN_BOOL(((*ce)->ce_flags & ZEND_ACC_INTERFACE) == 0);
+ RETURN_BOOL(((*ce)->ce_flags & (ZEND_ACC_INTERFACE | (ZEND_ACC_TRAIT - ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) == 0);
} else {
RETURN_FALSE;
}
}
/* }}} */
+/* {{{ proto bool trait_exists(string traitname [, bool autoload])
+ Checks if the trait exists */
+ZEND_FUNCTION(trait_exists)
+{
+ char *trait_name, *lc_name;
+ zend_class_entry **ce;
+ int trait_name_len;
+ int found;
+ zend_bool autoload = 1;
+ ALLOCA_FLAG(use_heap)
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &trait_name, &trait_name_len, &autoload) == FAILURE) {
+ return;
+ }
+
+ if (!autoload) {
+ char *name;
+ int len;
+
+ lc_name = do_alloca(trait_name_len + 1, use_heap);
+ zend_str_tolower_copy(lc_name, trait_name, trait_name_len);
+
+ /* Ignore leading "\" */
+ name = lc_name;
+ len = trait_name_len;
+ if (lc_name[0] == '\\') {
+ name = &lc_name[1];
+ len--;
+ }
+
+ found = zend_hash_find(EG(class_table), name, len+1, (void **) &ce);
+ free_alloca(lc_name, use_heap);
+ RETURN_BOOL(found == SUCCESS && (((*ce)->ce_flags & ZEND_ACC_TRAIT) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS));
+ }
+
+ if (zend_lookup_class(trait_name, trait_name_len, &ce TSRMLS_CC) == SUCCESS) {
+ RETURN_BOOL(((*ce)->ce_flags & ZEND_ACC_TRAIT) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
+ } else {
+ RETURN_FALSE;
+ }
+}
+/* }}} */
+
/* {{{ proto bool function_exists(string function_name)
Checks if the function exists */
--- /dev/null
+<?php
+
+trait AutoTrait {}
+
+?>
\ No newline at end of file
--- /dev/null
+--TEST--
+Test get_declared_traits() function : basic functionality
+--FILE--
+<?php
+/* Prototype : proto array get_declared_traits()
+ * Description: Returns an array of all declared traits.
+ * Source code: Zend/zend_builtin_functions.c
+ * Alias to functions:
+ */
+
+
+echo "*** Testing get_declared_traits() : basic functionality ***\n";
+
+trait MyTrait {}
+
+// Zero arguments
+echo "\n-- Testing get_declared_traits() function with Zero arguments --\n";
+var_dump(get_declared_traits());
+
+foreach (get_declared_traits() as $trait) {
+ if (!trait_exists($trait)) {
+ echo "Error: $trait is not a valid trait.\n";
+ }
+}
+
+echo "\n-- Ensure trait is listed --\n";
+var_dump(in_array('MyTrait', get_declared_traits()));
+
+echo "\n-- Ensure userspace interfaces are not listed --\n";
+interface I {}
+var_dump(in_array( 'I', get_declared_traits()));
+
+echo "\n-- Ensure userspace classes are not listed --\n";
+class MyClass {}
+var_dump(in_array( 'MyClass', get_declared_traits()));
+
+
+echo "Done";
+?>
+--EXPECTF--
+*** Testing get_declared_traits() : basic functionality ***
+
+-- Testing get_declared_traits() function with Zero arguments --
+array(%d) {
+%a
+}
+
+-- Ensure trait is listed --
+bool(true)
+
+-- Ensure userspace interfaces are not listed --
+bool(false)
+
+-- Ensure userspace classes are not listed --
+bool(false)
+Done
\ No newline at end of file
--- /dev/null
+--TEST--
+Test get_declared_traits() function : error conditions
+--FILE--
+<?php
+/* Prototype : proto array get_declared_traits()
+ * Description: Returns an array of all declared traits.
+ * Source code: Zend/zend_builtin_functions.c
+ * Alias to functions:
+ */
+
+echo "*** Testing get_declared_traits() : error conditions ***\n";
+
+// One argument
+echo "\n-- Testing get_declared_traits() function with one argument --\n";
+$extra_arg = 10;;
+var_dump( get_declared_traits($extra_arg) );
+
+echo "Done";
+?>
+--EXPECTF--
+*** Testing get_declared_traits() : error conditions ***
+
+-- Testing get_declared_traits() function with one argument --
+
+Warning: get_declared_traits() expects exactly 0 parameters, 1 given in %s on line 13
+NULL
+Done
--- /dev/null
+--TEST--
+Test get_declared_traits() function : testing autoloaded traits
+--FILE--
+<?php
+/* Prototype : proto array get_declared_traits()
+ * Description: Returns an array of all declared traits.
+ * Source code: Zend/zend_builtin_functions.c
+ * Alias to functions:
+ */
+
+
+echo "*** Testing get_declared_traits() : testing autoloaded traits ***\n";
+
+function __autoload($trait_name) {
+ require_once $trait_name . '.inc';
+}
+
+echo "\n-- before instance is declared --\n";
+var_dump(in_array('AutoTrait', get_declared_traits()));
+
+echo "\n-- after use is declared --\n";
+
+class MyClass {
+ use AutoTrait;
+}
+
+var_dump(in_array('AutoTrait', get_declared_traits()));
+
+echo "\nDONE\n";
+
+?>
+--EXPECTF--
+*** Testing get_declared_traits() : testing autoloaded traits ***
+
+-- before instance is declared --
+bool(false)
+
+-- after use is declared --
+bool(true)
+
+DONE
--- /dev/null
+--TEST--
+Test trait_exists() function : usage variations - case sensitivity
+--FILE--
+<?php
+/* Prototype : proto bool trait_exists(string traitname [, bool autoload])
+ * Description: Checks if the trait exists
+ * Source code: Zend/zend_builtin_functions.c
+ * Alias to functions:
+ */
+
+trait caseSensitivityTest {}
+var_dump(trait_exists('casesensitivitytest'));
+
+echo "Done"
+?>
+--EXPECTF--
+bool(true)
+Done
\ No newline at end of file
--- /dev/null
+--TEST--
+Test trait_exists() function : basic functionality
+--FILE--
+<?php
+/* Prototype : proto bool trait_exists(string traitname [, bool autoload])
+ * Description: Checks if the trait exists
+ * Source code: Zend/zend_builtin_functions.c
+ * Alias to functions:
+ */
+
+echo "*** Testing trait_exists() : basic functionality ***\n";
+
+function __autoload($traitName) {
+ echo "In __autoload($traitName)\n";
+}
+
+trait MyTrait {}
+
+echo "Calling trait_exists() on non-existent trait with autoload explicitly enabled:\n";
+var_dump( trait_exists('C', true) );
+echo "\nCalling trait_exists() on existing trait with autoload explicitly enabled:\n";
+var_dump( trait_exists('MyTrait', true) );
+
+echo "\nCalling trait_exists() on non-existent trait with autoload explicitly enabled:\n";
+var_dump( trait_exists('D', false) );
+echo "\nCalling trait_exists() on existing trait with autoload explicitly disabled:\n";
+var_dump( trait_exists('MyTrait', false) );
+
+echo "\nCalling trait_exists() on non-existent trait with autoload unspecified:\n";
+var_dump( trait_exists('E') );
+echo "\nCalling trait_exists() on existing trait with autoload unspecified:\n";
+var_dump( trait_exists('MyTrait') );
+
+echo "Done";
+?>
+--EXPECTF--
+*** Testing trait_exists() : basic functionality ***
+Calling trait_exists() on non-existent trait with autoload explicitly enabled:
+In __autoload(C)
+bool(false)
+
+Calling trait_exists() on existing trait with autoload explicitly enabled:
+bool(true)
+
+Calling trait_exists() on non-existent trait with autoload explicitly enabled:
+bool(false)
+
+Calling trait_exists() on existing trait with autoload explicitly disabled:
+bool(true)
+
+Calling trait_exists() on non-existent trait with autoload unspecified:
+In __autoload(E)
+bool(false)
+
+Calling trait_exists() on existing trait with autoload unspecified:
+bool(true)
+Done
\ No newline at end of file
--- /dev/null
+--TEST--
+Test trait_exists() function : error conditions (wrong number of arguments)
+--FILE--
+<?php
+/* Prototype : proto bool trait_exists(string traitname [, bool autoload])
+ * Description: Checks if the trait exists
+ * Source code: Zend/zend_builtin_functions.c
+ * Alias to functions:
+ */
+
+/**
+ * Test wrong number of arguments
+ */
+
+echo "*** Testing trait_exists() : error conditions ***\n";
+
+// Zero arguments
+echo "\n-- Testing trait_exists() function with Zero arguments --\n";
+var_dump( trait_exists() );
+
+//Test trait_exists with one more than the expected number of arguments
+echo "\n-- Testing trait_exists() function with more than expected no. of arguments --\n";
+$traitname = 'string_val';
+$autoload = true;
+$extra_arg = 10;
+var_dump( trait_exists($traitname, $autoload, $extra_arg) );
+
+echo "Done";
+?>
+--EXPECTF--
+*** Testing trait_exists() : error conditions ***
+
+-- Testing trait_exists() function with Zero arguments --
+
+Warning: trait_exists() expects at least 1 parameter, 0 given in %s on line 16
+NULL
+
+-- Testing trait_exists() function with more than expected no. of arguments --
+
+Warning: trait_exists() expects at most 2 parameters, 3 given in %s on line 23
+NULL
+Done
--- /dev/null
+--TEST--
+Test trait_exists() function : usage variations - unexpected types for agument 1
+--FILE--
+<?php
+/* Prototype : proto bool trait_exists(string traitname [, bool autoload])
+ * Description: Checks if the trait exists
+ * Source code: Zend/zend_builtin_functions.c
+ * Alias to functions:
+ */
+
+function __autoload($traitName) {
+ echo "In __autoload($traitName)\n";
+}
+
+function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) {
+ echo "Error: $err_no - $err_msg, $filename($linenum)\n";
+}
+set_error_handler('test_error_handler');
+
+echo "*** Testing trait_exists() : usage variations ***\n";
+
+// Initialise function arguments not being substituted (if any)
+$autoload = true;
+
+//get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+//array of values to iterate over
+$values = array(
+
+ // int data
+ 0,
+ 1,
+ 12345,
+ -2345,
+
+ // float data
+ 10.5,
+ -10.5,
+ 10.1234567e10,
+ 10.7654321E-10,
+ .5,
+
+ // array data
+ array(),
+ array(0),
+ array(1),
+ array(1, 2),
+ array('color' => 'red', 'item' => 'pen'),
+
+ // null data
+ NULL,
+ null,
+
+ // boolean data
+ true,
+ false,
+ TRUE,
+ FALSE,
+
+ // empty data
+ "",
+ '',
+
+ // object data
+ new stdclass(),
+
+ // undefined data
+ $undefined_var,
+
+ // unset data
+ $unset_var,
+);
+
+// loop through each element of the array for traitname
+
+foreach($values as $value) {
+ echo "\nArg value $value \n";
+ var_dump( trait_exists($value, $autoload) );
+};
+
+echo "Done";
+?>
+--EXPECTF--
+*** Testing trait_exists() : usage variations ***
+Error: 8 - Undefined variable: undefined_var, %s(67)
+Error: 8 - Undefined variable: unset_var, %s(70)
+
+Arg value 0
+In __autoload(0)
+bool(false)
+
+Arg value 1
+In __autoload(1)
+bool(false)
+
+Arg value 12345
+In __autoload(12345)
+bool(false)
+
+Arg value -2345
+In __autoload(-2345)
+bool(false)
+
+Arg value 10.5
+In __autoload(10.5)
+bool(false)
+
+Arg value -10.5
+In __autoload(-10.5)
+bool(false)
+
+Arg value 101234567000
+In __autoload(101234567000)
+bool(false)
+
+Arg value 1.07654321E-9
+In __autoload(1.07654321E-9)
+bool(false)
+
+Arg value 0.5
+In __autoload(0.5)
+bool(false)
+
+Arg value Array
+Error: 2 - trait_exists() expects parameter 1 to be string, array given, %s(77)
+NULL
+
+Arg value Array
+Error: 2 - trait_exists() expects parameter 1 to be string, array given, %s(77)
+NULL
+
+Arg value Array
+Error: 2 - trait_exists() expects parameter 1 to be string, array given, %s(77)
+NULL
+
+Arg value Array
+Error: 2 - trait_exists() expects parameter 1 to be string, array given, %s(77)
+NULL
+
+Arg value Array
+Error: 2 - trait_exists() expects parameter 1 to be string, array given, %s(77)
+NULL
+
+Arg value
+bool(false)
+
+Arg value
+bool(false)
+
+Arg value 1
+In __autoload(1)
+bool(false)
+
+Arg value
+bool(false)
+
+Arg value 1
+In __autoload(1)
+bool(false)
+
+Arg value
+bool(false)
+
+Arg value
+bool(false)
+
+Arg value
+bool(false)
+Error: 4096 - Object of class stdClass could not be converted to string, %s(76)
+
+Arg value
+Error: 2 - trait_exists() expects parameter 1 to be string, object given, %s(77)
+NULL
+
+Arg value
+bool(false)
+
+Arg value
+bool(false)
+Done
\ No newline at end of file
--- /dev/null
+--TEST--
+Test trait_exists() function : usage variations - unexpected types for agument 2
+--FILE--
+<?php
+/* Prototype : proto bool trait_exists(string traitname [, bool autoload])
+ * Description: Checks if the trait exists
+ * Source code: Zend/zend_builtin_functions.c
+ * Alias to functions:
+ */
+
+function __autoload($traitName) {
+ echo "In __autoload($traitName)\n";
+}
+
+function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) {
+ echo "Error: $err_no - $err_msg, $filename($linenum)\n";
+}
+set_error_handler('test_error_handler');
+
+echo "*** Testing trait_exists() : usage variations ***\n";
+
+// Initialise function arguments not being substituted (if any)
+$traitname = 'string_val';
+
+//get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+//array of values to iterate over
+$values = array(
+
+ // int data
+ 0,
+ 1,
+ 12345,
+ -2345,
+
+ // float data
+ 10.5,
+ -10.5,
+ 10.1234567e10,
+ 10.7654321E-10,
+ .5,
+
+ // array data
+ array(),
+ array(0),
+ array(1),
+ array(1, 2),
+ array('color' => 'red', 'item' => 'pen'),
+
+ // null data
+ NULL,
+ null,
+
+ // boolean data
+ true,
+ false,
+ TRUE,
+ FALSE,
+
+ // empty data
+ "",
+ '',
+
+ // string data
+ "string",
+ 'string',
+
+ // object data
+ new stdclass(),
+
+ // undefined data
+ $undefined_var,
+
+ // unset data
+ $unset_var,
+);
+
+// loop through each element of the array for autoload
+
+foreach($values as $value) {
+ echo "\nArg value $value \n";
+ var_dump( trait_exists($traitname, $value) );
+};
+
+echo "Done";
+?>
+--EXPECTF--
+*** Testing trait_exists() : usage variations ***
+Error: 8 - Undefined variable: undefined_var, %s(71)
+Error: 8 - Undefined variable: unset_var, %s(74)
+
+Arg value 0
+bool(false)
+
+Arg value 1
+In __autoload(string_val)
+bool(false)
+
+Arg value 12345
+In __autoload(string_val)
+bool(false)
+
+Arg value -2345
+In __autoload(string_val)
+bool(false)
+
+Arg value 10.5
+In __autoload(string_val)
+bool(false)
+
+Arg value -10.5
+In __autoload(string_val)
+bool(false)
+
+Arg value 101234567000
+In __autoload(string_val)
+bool(false)
+
+Arg value 1.07654321E-9
+In __autoload(string_val)
+bool(false)
+
+Arg value 0.5
+In __autoload(string_val)
+bool(false)
+
+Arg value Array
+Error: 2 - trait_exists() expects parameter 2 to be boolean, array given, %s(81)
+NULL
+
+Arg value Array
+Error: 2 - trait_exists() expects parameter 2 to be boolean, array given, %s(81)
+NULL
+
+Arg value Array
+Error: 2 - trait_exists() expects parameter 2 to be boolean, array given, %s(81)
+NULL
+
+Arg value Array
+Error: 2 - trait_exists() expects parameter 2 to be boolean, array given, %s(81)
+NULL
+
+Arg value Array
+Error: 2 - trait_exists() expects parameter 2 to be boolean, array given, %s(81)
+NULL
+
+Arg value
+bool(false)
+
+Arg value
+bool(false)
+
+Arg value 1
+In __autoload(string_val)
+bool(false)
+
+Arg value
+bool(false)
+
+Arg value 1
+In __autoload(string_val)
+bool(false)
+
+Arg value
+bool(false)
+
+Arg value
+bool(false)
+
+Arg value
+bool(false)
+
+Arg value string
+In __autoload(string_val)
+bool(false)
+
+Arg value string
+In __autoload(string_val)
+bool(false)
+Error: 4096 - Object of class stdClass could not be converted to string, %s(80)
+
+Arg value
+Error: 2 - trait_exists() expects parameter 2 to be boolean, object given, %s(81)
+NULL
+
+Arg value
+bool(false)
+
+Arg value
+bool(false)
+Done
\ No newline at end of file