|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #68882 __construct parameter not optional and won't accept null
Submitted: 2015-01-22 07:37 UTC Modified: 2021-06-02 13:20 UTC
From: evought at pobox dot com Assigned: cmb (profile)
Status: Wont fix Package: Weakref (PECL)
PHP Version: 5.5.20 OS: Fedora Linux 20
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-01-22 07:37 UTC] evought at pobox dot com
From manual page:
Weakref installed : 0.2.6 via pecl

The function reference lists the parameter as optional, however, it will neither accept null nor an empty argument list. If client code passes null, an error is issued stating that the first argument must be an object. If the argument is omitted, an error is issued that the method requires exactly one argument.

This seems to be either a documentation error, an API error, or both, but being able to set a weak reference which is initially invalid is a critical feature. Weak references are often (perhaps almost exclusively) used in caching situations where the reference may initially be invalid. As, clearly, the state may be invalidated at any time by the system, it makes little sense to prevent a null WeakRef from being instantiated (it has to be tested on every access anyway) and it does not make sense for the client code to have to test for nullity twice (once to see if the WeakRef is null and again to see if it has a null payload). The natural approach to stubbing the WeakRef for testing is to have it always have a null payload (forcing every read access to be a cache miss).

It would therefore be preferable for the constructor to accept null, possibly in addition to accepting no argument. Another way to solve this issue would be to remove the brackets from the manual (indicating that the parameter is always required) and define a named constant WeakRef which is used to indicate a payload which is never valid and for which get() will consistently return null.

As long as I have a way to initially specify a WeaKRef object property with an invalid payload, I am not bothered about how it is done.

Test script:
$ref1 = new \WeakRef();
\assert(null == $ref1.get());
$ref2 = new \WeakRef(null);
\assert(null == $ref2.get());

// Both produce errors on construction in my environment


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-22 12:54 UTC]
-Type: Bug +Type: Feature/Change Request
 [2015-01-22 12:54 UTC]
For now I fixed the documentation. The weakref construction was always meant to take a parameter.

I'm changing this bug to feature request to add the possibility to create an empty weakref directly by passing null.

Note that for now, you can do new WeakRef(new StdClass) which will immediately destroy the StdClass instance which should invalidate the weakref. This should allow you to do what you want, but I agree it is suboptimal.
 [2015-01-28 15:29 UTC]
-Assigned To: +Assigned To: colder
 [2017-10-24 07:25 UTC]
-Status: Assigned +Status: Open -Assigned To: colder +Assigned To:
 [2021-06-02 13:20 UTC]
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-06-02 13:20 UTC]
Since PECL/Weakref is no longer maintained (it is inherently
incompatible with PHP 7.3+), I'm closing this.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 21:01:33 2024 UTC