Req #14285 [Com]: Forcing variable declaration

From: Date: Sun, 15 Jan 2017 17:46:42 +0000
Subject: Req #14285 [Com]: Forcing variable declaration
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-206635@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=14285&edit=1 ID: 14285 Comment by: habibalex at netzero dot com Reported by: pnh102 at psu dot edu Summary: Forcing variable declaration Status: Closed Type: Feature/Change Request Package: Scripting Engine problem Operating System: * PHP Version: * Assigned To: krakjoe Block user comment: N Private report: N New Comment: The problem is when you use both the mistyped and correctly typed variables if they have both been initialized. function blah($correct)//correct variable name should be declared here { $correct = 1; $incorrect = 2;//this should throw an error because this is an undeclared variable $new = $incorrect*4;//this should throw an error because it's using an undeclared variable. return $new; } Previous Comments: ------------------------------------------------------------------------ [2016-02-16 12:53:46] azzaazza692 at hotmail dot com I think what was meant by the original question was something along the lines of: <?php option=explicit declare $typofree; $typofree = "hallo"; ... $xtypofree = "ooops"; ?> so as $xtypofree has not been 'declared' as a variable, it would throw a 'variable not declared' notice... ------------------------------------------------------------------------ [2013-10-27 15:33:50] krakjoe@php.net In modern PHP, you must declare static variables before they are assigned or referenced, which gives you a clear answer to the original question: Yes, use static variables. The comments here confuse the original question, and send everything awry ... This simply does not make sense: <?php $typofree = "hallo"; ... $xtypofree = "ooops"; ?> We cannot possibly raise a warning on the second variable assignment, it is perfectly valid code, with no way to tell when the programmer is misspelling or just programming the idea will simply not work, ever. I think the original question is probably born of a misunderstanding rather than a genuine desire to change the way PHP works, or did work. It doesn't make sense for you to have to declare _everything_ in PHP, so it doesn't make sense to have it as an option. Some conversations are so stupid, they are not worth having :) ------------------------------------------------------------------------ [2012-01-04 01:15:07] matt at mattandwell dot net This would be a great option to add in error_reporting, consider the following script. ini_set("error_reporting", E_ALL|E_STRICT); class test1 { private $m1 = "initial value in t1"; public function setVarsTest() { $this->m1 = "in t1"; } public function showValues() { echo(")".$this->m1."(<br/>"); } } class test2 extends test1 { public function setVarsTest() { $this->m1 = "in t2"; } } $t1 = new test1(); $t1->setVarsTest(); $t1->showValues(); $t2 = new test2(); $t2->setVarsTest(); $t2->showValues(); the output is: )in t1( )initial value in t1( changing $m1 to protected solves the issue, but tracking down issues like this after a refactoring session could be easier if PHP had this feature. "$t2->setVarsTest();" would generate a notice in the above code. ------------------------------------------------------------------------ [2008-06-13 11:27:34] gvdefence-ncr at yahoo dot it This feature is necessary in PHP. Try this simple example: error_reporting(E_ALL | E_NOTICE | E_STRICT); //E_STRICT for PHP5 class Test { var $mispelled_var; function Test($test) { $this->$mispeled_var = $test; /* ERROR: we think to instantiate $this->mispelled_var, but we forgot an 'l' and we are instantiating a new var. PHP does not raise any error or warning or notice. */ } }; All other languages would raise an error or at least a warning. I love PHP and I want it to grow, this is a neccesary feature. It is necessary to add a sort of gobal option that when activated makes PHP sensible to these type of errors. ------------------------------------------------------------------------ [2004-07-22 09:49:24] jeffrparsons at optushome dot com dot au I agree this is a very major issue, and it scares people I know away from using PHP. It would be extremely convenient to have warnings on assignment as well - instead of just upon reading a non-existent variable - and instead of creating a variable when a value is first assigned to it, require a separate declaration using a "var" keyword or such. Obviously not as standard, but as an option... ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=14285 -- Edit this bug report at https://bugs.php.net/bug.php?id=14285&edit=1

« previous php.bugs (#206635) next »