Global vs static variables in PHP

前端 未结 3 1384
梦谈多话
梦谈多话 2021-01-11 13:55

I\'m creating a basic framework in PHP. I need to pass data for the current page into different functions, allow them to modify and save it, and then pass it back to the pag

3条回答
  •  轮回少年
    2021-01-11 14:48

    For the record.

    Pro of static:

    Clarity of the code. For example:

    function fn() { 
       System::data()
     }
    

    versus

    function fn() { 
       global $system;
       $system->data()
     }
    

    Cons of static:

    • If you are using psr-4 then you must add (and include) a new class (and a new file). It impacts the performance even if you use opcache (opcache aleviates it but it's not magic).
    • You must define a block of code.

提交回复
热议问题