ChatGPT解决这个技术问题 Extra ChatGPT

What does PHP keyword 'var' do?

This is probably a very trivial question, but I haven't been able to find the answer neither through web search engines, nor on php.net. Please just direct me to where I can read about this, if you haven't got time to explain.

What does the 'var' keyword mean in PHP? Are there any differences between PHP4 and PHP5?

I guess when you asked this question on July 30th, 2009, this had not been published yet at php.net/manual/en/language.oop5.visibility.php?: "Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning." Or maybe you had not found it. But the answer is clearly stated at php.net.
That's really amazing to see what journeys PHP has passed these years!

A
Abraham Philip

It's for declaring class member variables in PHP4, and is no longer needed. It will work in PHP5, but will raise an E_STRICT warning in PHP from version 5.0.0 up to version 5.1.2, as of when it was deprecated. Since PHP 5.3, var has been un-deprecated and is a synonym for 'public'.

Example usage:

class foo {
    var $x = 'y'; // or you can use public like...
    public $x = 'y'; //this is also a class member variables.
    function bar() {
    }
}

"Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning." php.net/manual/en/language.oop5.visibility.php EDIT: I just saw that is has already been quoted in another answer. But you should edit yours accordingly.
This implies that it is necessary to use the public keyword with for a member variable. Is that true? Can't one simply put $x;?
so is it recommended to just stick with public/private and ignoring var entirely since we aren't worried about working with deprecated versions of PHP?
As of 2019: php-fig.org/psr/psr-12/#43-properties-and-constants states that var must not be used, and visibility must be declared on all properties.
G
Gumbo

The var keyword is used to declare variables in a class in PHP 4:

class Foo {
    var $bar;
}

With PHP 5 property and method visibility (public, protected and private) was introduced and thus var is deprecated.


In PHP 5.3 var is de-deprecated :)
If it's de-deprecated, what's the best practice now in 5.3 - to use it or not to use it? Can you use it like private var $foo = 'bar';?
Anyone know why it was de-deprecated?
It was undeprecated in 5.1.3 - the only reasons given are "for compatibility reasons". I think best practice is to use it only if you need to be compatible with PHP 4. Certainly don't do private var - that will break things real quick, as var is just a synonym for public in PHP 5.
var is not a true synonym for public as it cannot be used for declaring static members or constants.
2
2 revs, 2 users 80%

I quote from http://www.php.net/manual/en/language.oop5.visibility.php

Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.


I did tests. No returned E_STRICT ! sandbox.onlinephpfunctions.com/code/…
@WallacedeSouza, that's because you have used PHP 7 in your example. It was invalid only in versions 5.0 - 5.1.3
W
Webeng

Answer: From php 5.3 and >, the var keyword is equivalent to public when declaring variables inside a class.

class myClass {
  var $x;
}

is the same as (for php 5.3 and >):

class myClass {
  public $x;
}

History: It was previously the norm for declaring variables in classes, though later became depreciated, but later (PHP 5.3) it became un-depreciated.


k
kta

So basically it is an old style and do not use it for newer version of PHP. Better to use Public keyword instead;if you are not in love with var keyword. So instead of using

class Test {
    var $name;
}

Use

class Test {
   public $name;
}

why you say that? is there news of var being deprecated that I havent heard? the two should be synonymous
From the PHP manual: "The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword)." php.net/manual/en/language.oop5.visibility.php
k
kumar

var is used like public .if a varable is declared like this in a class var $a; if means its scope is public for the class. in simplea words var ~public

var $a;
public

x
xayer

In PHP7.3 still working...

https://www.php.net/manual/en/language.oop5.visibility.php

If declared using var, the property will be defined as public.


N
NappingRabbit

here and now in 2018 using var for variable declaration is synonymous with public as in

class Sample{
    var $usingVar;
    public $usingPublic;

    function .....

}