|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2001-11-29 11:17 UTC] pnh102 at psu dot edu
Is there a way to make PHP force you to declare variables like "option explicit" in VBScript or "use strict" in Perl? If not, will this feature become available? While this is not really a bug, it might be a nice feature to have available. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 11:00:02 2025 UTC |
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.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.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; }