php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81123 Setting a variable in a class to NULL does nothing
Submitted: 2021-06-10 13:10 UTC Modified: 2021-06-10 15:47 UTC
From: jplevene at gmail dot com Assigned:
Status: Suspended Package: Unknown/Other Function
PHP Version: 8.0.7 OS: Linux & Windows
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jplevene at gmail dot com
New email:
PHP Version: OS:

 

 [2021-06-10 13:10 UTC] jplevene at gmail dot com
Description:
------------
A declared variable in a class can't be set to NULL or unset

Test script:
---------------
class myClass {
	private $var = null;

	function __construct() {
		$this->var = "Hello";
	}

	public function func()
	{
		$this->var = null;  // Does nothing, still "Hello"
		unset($this->var);  // Does nothing, still "Hello"
		$this->var = false; // Works
	}
}

Expected result:
----------------
Variable should be null

Actual result:
--------------
Variable stays as "Hello"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-06-10 13:13 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2021-06-10 13:13 UTC] nikic@php.net
How did you determine that the value is still "Hello"? Please provide a complete example which also shows the incorrect result.
 [2021-06-10 14:22 UTC] jplevene at gmail dot com
-Status: Feedback +Status: Open
 [2021-06-10 14:22 UTC] jplevene at gmail dot com
public function func()
	{
		$this->var = null;
		print_r($this->var);// Does nothing, still "Hello"
		unset($this->var);
		print_r($this->var);// Does nothing, still "Hello"
		$this->var = false;
		print_r($this->var);// Works and prints "Hello"
	}
 [2021-06-10 14:27 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
is it *really* that hard to provide a *full working* reproducer instead some useless comments?

your comments are simply not true

[user@localhost:/downloads]$ cat test.php
<?php
$test = new myClass;
$test->func();
class myClass {
        private $var = null;

        function __construct() {
                $this->var = "Hello";
        }

        public function func()
        {
                $this->var = null;  // Does nothing, still "Hello"
                echo $this->var, "\n";
                unset($this->var);  // Does nothing, still "Hello"
                echo $this->var, "\n";
                $this->var = false; // Works
        }
}
[user@localhost:/downloads]$ php test.php


Notice: Undefined property: myClass::$var in /mnt/data/downloads/test.php on line 16

[user@localhost:/downloads]$
 [2021-06-10 14:44 UTC] imsop@php.net
Please ignore the troll, but running your script on an online sandbox doesn't reproduce the problem: https://3v4l.org/POdHF (I've switched to var_dump just because it shows the difference between NULL and false more clearly).

Does running that exact code on your local install gives a different result, or is there some other code that's interacting in some way?
 [2021-06-10 14:47 UTC] cmb@php.net
-Status: Open +Status: Feedback
 [2021-06-10 15:12 UTC] jplevene at gmail dot com
-Status: Feedback +Status: Open
 [2021-06-10 15:12 UTC] jplevene at gmail dot com
It's in a far bigger class that I discovered this.  I'll run some tests now and get back to you.
 [2021-06-10 15:33 UTC] jplevene at gmail dot com
-Status: Open +Status: Closed
 [2021-06-10 15:33 UTC] jplevene at gmail dot com
In a simple class I can't reproduce it, but in my larger one it fails 100% of the time.

I did a workaround as setting it to false worked fine, so I left it at that.
 [2021-06-10 15:47 UTC] imsop@php.net
-Status: Closed +Status: Suspended
 [2021-06-10 15:47 UTC] imsop@php.net
It sounds like there's something else in that class that's interacting with your code then - perhaps some magic using Property Overloading (__set, __unset, etc): https://www.php.net/manual/en/language.oop5.overloading.php

Please come back to us if you find a way to reproduce the issue and still believe it is a bug with PHP.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 03 20:01:29 2024 UTC