php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #16479 var declaration in class has no meaning?
Submitted: 2002-04-07 18:31 UTC Modified: 2003-01-02 19:48 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: vdhome at idas dot cz Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.0.6 OS: Win98SE
Private report: No CVE-ID: None
 [2002-04-07 18:31 UTC] vdhome at idas dot cz
I am not sure whether this is a bug, a feature, or lack of a feature. Perhaps this should be marked a "Feature Request" then... Anyway, here goes: the var declaration in a class seems to have no meaning whatsoever, except perhaps for documentation purposes. PHP allows me to assign to class variables that weren't declared without even a warning at the highest warning reporting level. That's actually not very surprising since PHP doesn't otherwise have declarations of variables, but what is then the meaning of var in classes?

I suggest that PHP should at least warn about class variables that weren't declared.

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-08 01:29 UTC] mfischer@php.net
Well, you have get_class_vars() which can retrieve all the default properties (i.e. the ones declared with 'var $foo').

And your last suggestions is against the nature of PHP. You just do not have to declare variables to use it.

Making this a doc problem though I'm not quite sure; letting the doc team decide.
 [2002-04-08 15:49 UTC] vdhome at idas dot cz
Yeah, I guess I asked for it. But then I'd have to argue that the var declaration itself is against the nature of PHP! Firstly because it's a declaration, and secondly because it's useless.

Anyway, I understand that there will be major changes in OOP in ZendEngine2, so this is probably not relevant anymore. BTW, when is this ZE2 scheduled to launch? Will it be already in 4.2.0 which is now in RC stage?
 [2002-04-22 16:32 UTC] ricko at garagegames dot com
So is it (and will it continue to be) valid to use/create class variables not explicitly declared with var?

Currently the following produce the excact same results except that the varaible 'a' will not be returned by get_class_vars.

class foo
{
   var $a;
   function foo()
   {
      $this->a = "test";
   }
};

class foo2
{
   function foo2()
   {
      $this->a = "test";
   }
};
 [2002-11-13 08:55 UTC] php at popbandetleif dot cjb dot net
<?
  /**
   * A bugg in php?
   */

   class One {
	   var $a;
	   var $b;

	   /** Print $a & $b */
	   function printVars() {
		    echo("Vareabels: <BR>\n");
			echo("\$a: " . $this->$a . "<BR>\n");
			echo("\$b: " . $this->$b . "<BR>\n");
	   }
   }

   class Two extends One {
		function Two() {
			$this->$a = "A";
			$this->$b = "B";
			$this->printVars();
		}
   }

   $two = new Two;

   echo("<BR>\n");

   print_r(get_object_vars($two));
?>

This code gave the result:

Vareabels:
$a: B
$b: B

Array ( [a] => [b] => [] => B )

Should to work this way?
 [2002-11-14 06:23 UTC] hholzgra@php.net
$this->$a = "A"; // ????

didn't you mean to write 

$this->a = "A";

mind the extra $ in your code, this lead
to "variable variable" expansion, and as
$a was undefined (empty) assingt to 
a member variable named ""

so yes, it should work this way!
 [2003-01-02 19:48 UTC] nicos@php.net
Documentations are clear on that.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Aug 16 18:01:27 2024 UTC