Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Zend/tests/hashkey/001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
testing hash returns string
--FILE--
<?php
class Foo {
public function __hash() {
return __CLASS__;
}
}

$foo = new Foo();
$test = [
$foo => true
];

var_dump($test);
?>
--EXPECT--
array(1) {
["Foo"]=>
bool(true)
}


26 changes: 26 additions & 0 deletions Zend/tests/hashkey/002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
testing hash inheritance
--FILE--
<?php
class Foo {
public function __hash() {
return static::class;
}
}

class Bar extends Foo {}

$bar = new Bar();
$test = [
$bar => true
];

var_dump($test);
?>
--EXPECT--
array(1) {
["Bar"]=>
bool(true)
}


24 changes: 24 additions & 0 deletions Zend/tests/hashkey/003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
testing hash returns true
--FILE--
<?php
class Foo {
public function __hash() {
return true;
}
}

$foo = new Foo();
$test = [
$foo => true
];

var_dump($test);
?>
--EXPECT--
array(1) {
[1]=>
bool(true)
}


24 changes: 24 additions & 0 deletions Zend/tests/hashkey/004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
testing hash returns false
--FILE--
<?php
class Foo {
public function __hash() {
return false;
}
}

$foo = new Foo();
$test = [
$foo => true
];

var_dump($test);
?>
--EXPECT--
array(1) {
[0]=>
bool(true)
}


24 changes: 24 additions & 0 deletions Zend/tests/hashkey/005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
testing hash returns null
--FILE--
<?php
class Foo {
public function __hash() {
return null;
}
}

$foo = new Foo();
$test = [
$foo => true
];

var_dump($test);
?>
--EXPECT--
array(1) {
[""]=>
bool(true)
}


23 changes: 23 additions & 0 deletions Zend/tests/hashkey/006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
testing hash returns object
--FILE--
<?php
class Foo {
public function __hash() {
return new stdClass();
}
}

$foo = new Foo();
$test = [
$foo => true
];

var_dump($test);
?>
--EXPECTF--
Warning: Illegal offset type in %s on line %d
array(0) {
}


23 changes: 23 additions & 0 deletions Zend/tests/hashkey/007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
testing hash returns array
--FILE--
<?php
class Foo {
public function __hash() {
return [];
}
}

$foo = new Foo();
$test = [
$foo => true
];

var_dump($test);
?>
--EXPECTF--
Warning: Illegal offset type in %s on line %d
array(0) {
}


24 changes: 24 additions & 0 deletions Zend/tests/hashkey/008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
testing hash returns double
--FILE--
<?php
class Foo {
public function __hash() {
return 10.1;
}
}

$foo = new Foo();
$test = [
$foo => true
];

var_dump($test);
?>
--EXPECT--
array(1) {
[10]=>
bool(true)
}


24 changes: 24 additions & 0 deletions Zend/tests/hashkey/009.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
testing hash returns long
--FILE--
<?php
class Foo {
public function __hash() {
return 10;
}
}

$foo = new Foo();
$test = [
$foo => true
];

var_dump($test);
?>
--EXPECT--
array(1) {
[10]=>
bool(true)
}


23 changes: 23 additions & 0 deletions Zend/tests/hashkey/010.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
testing hash returns resource
--FILE--
<?php
class Foo {
public function __hash() {
return fopen("php://stdin", "r");
}
}

$foo = new Foo();
$test = [
$foo => true
];

var_dump($test);
?>
--EXPECTF--
Warning: Illegal offset type in %s on line 11
array(0) {
}


48 changes: 48 additions & 0 deletions Zend/tests/hashkey/011.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
testing hash returns string, different ops
--FILE--
<?php
class Foo {
public function __hash() {
return __CLASS__;
}
}

$foo = new Foo();
$test = [
$foo => true,
"bar" => false,
];

var_dump($test);
var_dump($test[$foo]);
var_dump(isset($test[$foo]));
unset($test[$foo]);
var_dump($test);
var_dump(empty($test[$foo]));
$test[$foo] = 42;
$test[$foo]++;
var_dump($test);
?>
--EXPECT--
array(2) {
["Foo"]=>
bool(true)
["bar"]=>
bool(false)
}
bool(true)
bool(true)
array(1) {
["bar"]=>
bool(false)
}
bool(true)
array(2) {
["bar"]=>
bool(false)
["Foo"]=>
int(43)
}


3 changes: 2 additions & 1 deletion Zend/zend.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ struct _zend_class_entry {
union _zend_function *__call;
union _zend_function *__callstatic;
union _zend_function *__tostring;
union _zend_function *__hash;
union _zend_function *__debugInfo;
union _zend_function *serialize_func;
union _zend_function *unserialize_func;

zend_class_iterator_funcs iterator_funcs;

/* handlers */
Expand Down
13 changes: 13 additions & 0 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,15 @@ ZEND_API zval *add_get_index_stringl(zval *arg, zend_ulong index, const char *st
ZEND_API int array_set_zval_key(HashTable *ht, zval *key, zval *value TSRMLS_DC) /* {{{ */
{
zval *result;
zval obj_key;
int free_key = 0;

if (UNEXPECTED(Z_TYPE_P(key) == IS_OBJECT && Z_OBJCE_P(key)->__hash)) {
if (zend_object_offset(key, &obj_key TSRMLS_CC) == SUCCESS) {
free_key = 1;
key = &obj_key;
}
}

switch (Z_TYPE_P(key)) {
case IS_STRING:
Expand Down Expand Up @@ -1684,6 +1693,10 @@ ZEND_API int array_set_zval_key(HashTable *ht, zval *key, zval *value TSRMLS_DC)
result = NULL;
}

if(free_key) {
zval_dtor(&obj_key);
}

if (result) {
if (Z_REFCOUNTED_P(result)) {
Z_ADDREF_P(result);
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ typedef struct _zend_fcall_info_cache {
class_container.__set = handle_propset; \
class_container.__unset = handle_propunset; \
class_container.__isset = handle_propisset; \
class_container.__hash = NULL; \
class_container.__debugInfo = NULL; \
class_container.serialize_func = NULL; \
class_container.unserialize_func = NULL; \
Expand Down
7 changes: 7 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,7 @@ ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify
ce->__call = NULL;
ce->__callstatic = NULL;
ce->__tostring = NULL;
ce->__hash = NULL;
ce->create_object = NULL;
ce->get_iterator = NULL;
ce->iterator_funcs.funcs = NULL;
Expand Down Expand Up @@ -4066,6 +4067,12 @@ void zend_begin_method_decl(zend_op_array *op_array, zend_string *name, zend_boo
"public visibility and cannot be static");
}
ce->__tostring = (zend_function *) op_array;
} else if (zend_string_equals_literal(lcname, ZEND_HASH_FUNC_NAME)) {
if (!is_public || is_static) {
zend_error(E_WARNING, "The magic method __hash() must have "
"public visibility and cannot be static");
}
ce->__hash = (zend_function *) op_array;
} else if (zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME)) {
if (!is_public || is_static) {
zend_error(E_WARNING, "The magic method __invoke() must have "
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ END_EXTERN_C()
#define ZEND_CALL_FUNC_NAME "__call"
#define ZEND_CALLSTATIC_FUNC_NAME "__callstatic"
#define ZEND_TOSTRING_FUNC_NAME "__tostring"
#define ZEND_HASH_FUNC_NAME "__hash"
#define ZEND_AUTOLOAD_FUNC_NAME "__autoload"
#define ZEND_INVOKE_FUNC_NAME "__invoke"
#define ZEND_DEBUGINFO_FUNC_NAME "__debuginfo"
Expand Down
Loading