# 5.3 compile time still depends on runtime constants ("Default value for parameters with a class type hint can only be NULL")

- Thread: https://externals.io/message/42219
- Original archive: https://news-web.php.net/php.internals/42219
- Started by phpxcache on 2008-12-12T05:21:32+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #42219 — phpxcache — 2008-12-12T05:21:32+00:00

URL: https://externals.io/message/42219#42219

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?

---

## #42220 — Sebastian Bergmann — 2008-12-12T05:34:39+00:00

URL: https://externals.io/message/42219#42220 · In reply to: #42219

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

---

## #42221 — moo.tinys — 2008-12-12T07:16:19+00:00

URL: https://externals.io/message/42219#42221 · In reply to: #42220

sorry about that. i didn't mean to do any ad by using special nick
name here. it's... just relatived to opcode cachers

---

## #42222 — Stanislav Malyshev — 2008-12-12T07:35:05+00:00

URL: https://externals.io/message/42219#42222 · In reply to: #42219

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

---

## #42323 — phpxcache — 2008-12-18T02:05:10+00:00

URL: https://externals.io/message/42219#42323 · In reply to: #42222

sorry, forgot to "reply to all"
http://bugs.php.net/bug.php?id=46850
