php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #46348 constant docs still say you can't define a resource as a constant, but you can
Submitted: 2008-10-20 20:47 UTC Modified: 2008-12-02 00:21 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: matt at waggoner dot com Assigned: kalle (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: N/A
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: matt at waggoner dot com
New email:
PHP Version: OS:

 

 [2008-10-20 20:47 UTC] matt at waggoner dot com
Description:
------------
In the PHP Language Reference section for Constants at http://us3.php.net/constants it says the following:

"Only scalar data (boolean, integer, float and string) can be contained in constants. Do not define resource constants."

This is false in both 5.2.5 and 5.2.6.  Resources (DB connections, file handles, etc.) can be defined as constants and will function.

Either the documentation should be changed to match the code, or the code should be fixed to match the documentation.  Bug #29534 seems to say that the documentation was fixed a couple of years ago, but the docs seem to still be incorrect.

Reproduce code:
---------------
foo.php:

<?php
	error_reporting(E_ALL);

	$link = fopen("foo.php", "r");
	var_dump($link);

	define('FOO', $link);

	$x = fread(FOO, 10);
	var_dump($x);
	echo(gettype(FOO));

	fclose(FOO);
?>

Expected result:
----------------
I'd expect an error on line 7, telling me that resources cannot be defined as constants.

Actual result:
--------------
Hello!
resource(5) of type (stream)
string(10) "Hello!
<?p"
resource

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-20 20:54 UTC] matt at waggoner dot com
Ignore that "Hello!" in the Actual Result section, that was pasted from the wrong place.  The proper Actual Result is:

resource(5) of type (stream)
string(10) "Hello!
<?p"
resource
 [2008-11-04 16:43 UTC] vrana@php.net
Documentation doesn't say it's not possible. It just says to not do it because it can cause certain problems.
 [2008-11-05 04:27 UTC] kalle@php.net
Jakub, im reopening this bug as I need to put on a notice after the decision thats currently going on internals is final :)
 [2008-11-05 05:15 UTC] matt at waggoner dot com
The documentation *does* say it's not possible:

"Only scalar data (boolean, integer, float and string) can be contained in constants."

I don't know how that can be interpreted to say that resources *can* be contained in constants.  Granted, the next sentence ambiguates things:

"Do not define resource constants."

That phrasing implies that you can, but must not, define resource constants.  If you absolutely could not, it would say "Resource constants cannot be defined" or something equivalent.  However, even in this interpretation, the two sentences contradict each other: the first one explicitly says that you CANNOT define resource constants, but the second one explicitly says that you MUST NOT define resource constants, and implies that it is possible.  Either the documentation must be changed to unambiguously reflect reality, or the code needs to be changed to reflect the first sentence, and the second sentence must be removed.

If we only change the documentation, I suggest the following wording:

"Only scalar data (boolean, integer, float and string) should be contained in constants.  Resources can be defined as constants, but you should not do this."  The problem is that it doesn't explain WHY the code allows resources to be defined as constants, even though the documentation is telling you not to.
 [2008-12-02 00:21 UTC] kalle@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2020-02-07 06:10 UTC] phpdocbot@php.net
Automatic comment on behalf of kalle
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=a0af9fd50a1136de09e427f00b8404a233da9521
Log: Fixed #46348 (constant docs still say you can't define a resource as a constant, but you can)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Apr 28 23:01:28 2025 UTC