PHP Variable vs Array vs Object

前端 未结 7 1367
忘掉有多难
忘掉有多难 2020-12-31 18:03

This is probably considered a really silly question, but I\'m in the process of putting together a simple template system for a website and am trying to keep track of my var

7条回答
  •  执念已碎
    2020-12-31 18:51

    In ideal scenarios every variable should belong to an object, other than the variables local to methods for temp purposes. However we don't live in an ideal world and specially our programming languages are far from it. Based on what the situation is, choose the best way to go about it to make your life easier. If you are using things for templates, generally you keep all the data in an array and extract the array to get stand alone variables.

    So yeah, the object method is the nicest, try to make it happen as much as you can without spending crazy amounts of time in doing it.

    Also if you love objects and want to have the neat -> way of doing it, you can do

    $object = (object)$array;
    

    That would convert your array to an object.

    Hope that helps.

提交回复
热议问题