dqnek0079 2012-06-22 21:27
浏览 14
已采纳

PHP静态类成员范围

Let me preface this with, I come from a Java background.

What is the scope of a static class member in PHP? ie: Request, Session, Server Lifecycle, etc

My current understanding is that everything is Request unless it is stuck on the session. I have found nothing in the Language Guide to refute or confirm this.

Example

class MyKlass {
    public static $K_PAGE_SIZE = 50;
    public static $K_WITH_SPRINKLES = true;
}

if (isset($_GET['NO_SPRINKLES'])) {
    MyKlass::$K_WITH_SPRINKLES = false;
}

var_dump(MyKlass::$K_WITH_SPRINKLES);

Case 1

If I were to visit a page with this code with nothing in the query line I should see

bool(true)

Case 2

If I were to visit the page with this code and ?NO_SPRINKLES=true in the query line, I should see

bool(false)

Case 3

If I visit the page with ?NO_SPRINKLES=true in the query line and then visit the page without it, I should always see bool(true) right?

Case 4

After visiting the page with ?NO_SPRINKLES=true, others who visit the page should still see bool(true) correct?

  • 写回答

2条回答 默认 最新

  • dongpu1908 2012-06-22 21:32
    关注

    PHP wont retain any information unless you make use of sessions. So a variable is created at the begining of the code when you're loading the page, and destroyed at the end of the code, when it's sent.

    Same with classes and their members. You can edit them as much as you want, it'll retain information, but at the very end of your script, it'll be lost.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应