php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77644 [preload] Uncaught Error: Undefined constant
Submitted: 2019-02-21 01:44 UTC Modified: 2019-02-25 20:02 UTC
From: mberchtold at gmail dot com Assigned: dmitry (profile)
Status: Not a bug Package: opcache
PHP Version: master-Git-2019-02-21 (snap) OS: Windows 10 x64
Private report: No CVE-ID: None
 [2019-02-21 01:44 UTC] mberchtold at gmail dot com
Description:
------------
When a class that uses a constant with a namespace prefix is preloaded, using the class results in a fatal error.

Test script:
---------------
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=-1
opcache.preload={PWD}/preload_undef_const_4.inc

preload_undef_const_4.inc:

<?php

namespace mytest;

define(__NAMESPACE__ . '\CONSTANTINE', 'value');

class Test
{
	public function __construct()
	{
		echo CONSTANTINE;
	}
}

--FILE--
<?php
new \mytest\Test();


Expected result:
----------------
no error

Actual result:
--------------
PHP Fatal error:  Uncaught Error: Undefined constant 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-02-21 08:39 UTC] cmb@php.net
-Summary: PHP Fatal error: Uncaught Error: Undefined constant +Summary: [preload] Uncaught Error: Undefined constant
 [2019-02-21 09:08 UTC] nikic@php.net
-Assigned To: +Assigned To: dmitry
 [2019-02-21 09:08 UTC] nikic@php.net
I believe that constants are not preloaded intentionally. @dmitry: Can you explain why constants cannot be preloaded?
 [2019-02-22 11:57 UTC] dmitry@php.net
-Status: Assigned +Status: Not a bug
 [2019-02-22 11:57 UTC] dmitry@php.net
Preloading works only with functions and classes (not with whole scripts).

Constants in PHP always handled at run-time. 

define() is a function that side effect can't be cached, because of possible re-defintion.

See https://wiki.php.net/rfc/constant_redefinition

For now, I mark this as "Not a bug".
 [2019-02-22 15:36 UTC] mberchtold at gmail dot com
It fails at runtime and not during preloading. I dont see how this is not a bug.
 [2019-02-22 17:34 UTC] mberchtold at gmail dot com
Maybe you could prevent preloading of classes with undefined constants?
 [2019-02-25 20:02 UTC] dmitry@php.net
The class "Test" is preloaded, but the script still has to be included or required (it's cached by opcache).

<?php
include("preload_undef_const_4.inc");
new \mytest\Test();
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Aug 14 20:01:24 2020 UTC