關於PHP中方法中的static的問題

來源:互聯網
上載者:User
static用在類的方法中,我現在的問題是static的這個變數是儲存在記憶體中,
1 具體儲存在記憶體中的哪個地方?
2 這個static的變數多長時間被收回?
3 過多的使用的static會不會造成伺服器記憶體不夠?
4 不同的使用者的static變數為什麼會互不影響?

回複內容:

static用在類的方法中,我現在的問題是static的這個變數是儲存在記憶體中,
1 具體儲存在記憶體中的哪個地方?
2 這個static的變數多長時間被收回?
3 過多的使用的static會不會造成伺服器記憶體不夠?
4 不同的使用者的static變數為什麼會互不影響?

When you declare a class method/variable as static, it is bound to and shared by the class, not the object. From a memory management perspective what this means is that when the class definition is loaded into the heap memory, these static objects are created there. When the class's actual object is created in the stack memory and when updates on the static properties are done, the pointer to the heap which contains the static object gets updated. This does help to reduce memory but not by much.

From a programming paradigm, people usually choose to use static variables for architectural advantages more than memory management optimization. In other words, one might create static variables like you mentioned, when one wants to implement a singleton or factory pattern. It provides more powerful ways of knowing what is going on at a "class" level as opposed to what transpires at an "object" level.

static的是儲存在記憶體中的吧,過多的使用應該會造成記憶體不足?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.