FAQ
this is a discussion about "Default value for parameters with a class
type hint can only be NULL" error

let's focus our eyes on
==== zend_compile.c function zend_do_receive_arg
void zend_do_receive_arg(zend_uchar op, const znode *var, const znode
*offset, const znode *initialization, znode *class_type, const znode
*varname, zend_uchar pass_by_reference TSRMLS_DC)
{
...........
if (op == ZEND_RECV_INIT) {
if (Z_TYPE(initialization->u.constant) == IS_NULL ||
(Z_TYPE(initialization->u.constant) == IS_CONSTANT &&
!strcasecmp(Z_STRVAL(initialization->u.constant), "NULL"))) {
cur_arg_info->allow_null = 1;
} else {
zend_error(E_COMPILE_ERROR, "Default value for
parameters with a class type hint can only be NULL");
}
}
====test.php======
<?php
namespace My\NS;

use My\NS;

class A {
public function test(A $obj=null) {
var_dump($obj);
}
}
================

(the following gdb input/output is still using macro for your reading,
expand the macro if you want to execute)

test case 1
precondition: CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION;
break at function zend_do_receive_arg and
(gdb) print Z_TYPE(initialization->u.constant) == IS_NULL
1 (true)
which means it still "subst null to IS_NULL"

test case 2
precondtion: let's assume ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION is
working for zend_do_receive_arg, simply empty hash table
EG(zend_constants) = &an_empty_hash_table before zend_compile_file
break at function zend_do_receive_arg and
(gdb) print Z_TYPE(initialization->u.constant) == IS_NULL
0 (false)
(gdb) print Z_TYPE(initialization->u.constant) == IS_CONSTANT
0 (false)

so what is that?
(gdb) print Z_TYPE(initialization->u.constant)
24
(gdb) print (Z_TYPE(initialization->u.constant) &
IS_CONSTANT_TYPE_MASK) == IS_CONSTANT
1 (true)
************ ok, we get the first bug
(gdb) p !strcasecmp(Z_STRVAL(initialization->u.constant), "NULL"))
0 (false)
why?
(gdb) p Z_STRVAL(initialization->u.constant)
"My\\NS\\null"
this is the reason. looks likestrcasecmp is not enough here at compile
time. or you could just handle abc(array $a = null) as a special case?

Search Discussions

  • Sebastian Bergmann at Dec 12, 2008 at 5:34 am

    phpxcache wrote:
    [...]
    Could you please have the decency / show us the courtesy of posting
    using a real name?

    kthxbye,
    Sebastian

    --
    Sebastian Bergmann http://sebastian-bergmann.de/
    GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
  • Moo . tinys at Dec 12, 2008 at 7:16 am
    sorry about that. i didn't mean to do any ad by using special nick
    name here. it's... just relatived to opcode cachers
  • Stanislav Malyshev at Dec 12, 2008 at 7:35 am
    Hi!
    (gdb) p Z_STRVAL(initialization->u.constant)
    "My\\NS\\null"
    this is the reason. looks likestrcasecmp is not enough here at compile
    time. or you could just handle abc(array $a = null) as a special case?
    Looks like a bug, could you submit a bug report (with reproducing code)
    to bugs.php.net?
    --
    Stanislav Malyshev, Zend Software Architect
    stas@zend.com http://www.zend.com/
    (408)253-8829 MSN: stas@zend.com
  • Phpxcache at Dec 18, 2008 at 2:05 am
    sorry, forgot to "reply to all"
    http://bugs.php.net/bug.php?id=46850

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedDec 12, '08 at 5:21a
activeDec 18, '08 at 2:05a
posts5
users4
websitephp.net

People

Translate

site design / logo © 2022 Grokbase