When would you need to use late static binding?

前端 未结 6 807
遥遥无期
遥遥无期 2020-12-16 04:33

After reading this description of late static binding (LSB) I see pretty clearly what is going on. Now, under which sorts of circumstances might that be most useful or neede

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 04:48

    Suppose you have classes representing tables (row instances) in a simplified object-relational mapper. You would have a class "User" and a class "Company" who's instances are representing rows of the respective tables. User and Company would inherit from some base abstract class, let's say "BaseObject" that will have some common methods like save(), delete(), validate() etc ...

    If you want to store data about the validation and the table definition, the best place would be in a static variable in each derived class - since the validation and table definition is the same for each instance of User.

    Without LSB the mentioned validate() method in BaseObject would have no reference to the static variables defined in User and Company, even though you are calling it through an instance of User. It will look for the same static variable in the BaseObject class, and it will raise an error.

    This is my experience with PHP 5.2.8 - LSB is going to be introduced in 5.3

提交回复
热议问题