為什麼 php 類別中成員變數前面需要冠上 var 字串 What does PHP keyword ‘var’ do?

From: http://stackoverflow.com/questions/1206105/what-does-php-keyword-var-do

這是用在 php4 的類別成員變數的宣告,也適用於 php5 。但因為 php5 配置較為嚴僅(E_STRICT)的關係 冠上 var 會被提出警告。

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 which it has been deprecated.

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() {}
}

var keyword 是在 PHP4 中宣告類別成員變數用的,而在 PHP5 中則改用 public、protext、private, 當然還是可以使用var宣告成員變數,但是不建議這樣做。

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.

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s