PHP 关键字“var”有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1206105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:32:34  来源:igfitidea点击:

What does PHP keyword 'var' do?

phpkeyword

提问by joelpet

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.

这可能是一个非常微不足道的问题,但我无法通过网络搜索引擎或 php.net 找到答案。如果您没有时间解释,请直接将我指向我可以阅读的地方。

  1. What does the 'var' keyword mean in PHP?
  2. Are there any differences between PHP4 and PHP5?
  1. PHP 中的“var”关键字是什么意思?
  2. PHP4 和 PHP5 之间有什么区别吗?

采纳答案by karim79

It's for declaring class member variables in PHP4, and is no longer needed. It will work in PHP5, but will raise an E_STRICTwarning 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'.

它用于在 PHP4 中声明类成员变量,不再需要。它可以在 PHP5 中工作,但会E_STRICT在 PHP 5.0.0 到 5.1.2 版本中引发警告,因为它被弃用了。自 PHP 5.3 起,var 不再被弃用,它是“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() {
    }
}

回答by Gumbo

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

var关键字用于在 PHP 4 中声明类中的变量

class Foo {
    var $bar;
}

With PHP 5 property and method visibility(public, protectedand private) was introduced and thus varis deprecated.

在 PHP 5中引入了属性和方法可见性public,protectedprivate),因此var不推荐使用。

回答by taatparya

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

我引用自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_STRICTwarning.

注意:出于兼容性原因(作为 public 关键字的同义词),仍然支持 PHP 4 使用 var 关键字声明变量的方法。在 PHP 5 5.1.3 之前,它的使用会产生E_STRICT警告。

回答by Webeng

Answer:From php5.3 and >, the varkeyword is equivalent to publicwhen declaring variables inside a class.

答:php5.3 and 开始>var关键字相当于public在类中声明变量时。

class myClass {
  var $x;
}

is the same as (for php5.3 and >):

与(对于php5.3 和>)相同:

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.

历史:以前是在类中声明变量的规范,虽然后来贬值了,但后来(PHP 5.3)它变得不贬值了。

回答by 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

所以基本上它是一种旧样式,不要将它用于较新版本的 PHP。最好改用 Public 关键字;如果您不喜欢 var 关键字。所以而不是使用

class Test {
    var $name;
}

Use

class Test {
   public $name;
}

回答by xayer

In PHP7.3 still working...

在 PHP7.3 中仍然有效...

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

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

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

如果使用 var 声明,则该属性将定义为 public。

回答by 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 像 public 一样使用。如果一个变量在类 var $a 中是这样声明的;if 表示它的范围是该类的公共范围。简单来说 var ~public

var $a;
public

回答by NappingRabbit

here and now in 2018using varfor variable declaration is synonymous with publicas in

这里和现在2018使用var的变量声明是同义的public,如

class Sample{
    var $usingVar;
    public $usingPublic;

    function .....

}