|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 07:00:01 2025 UTC |
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" }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]$