Object Oriented PHP: When is it applicable?


48 观看


4 作者的声誉

Currently in my first development position working with a significant amount of legacy data and applications. Because of that, we only write in procedural PHP. I know objects are a more desirable skill, so I'm actively trying to learn, just for the sake of knowing.

I am confused, however, as to when objects are applicable. I'm under the impression they're used for repetitive code and take the place of common functions. Outside of building classes, is the code base still procedural? I suppose when I think about OO PHP, I'm thinking I'm missing an entirely difference coding style used throughout the source.

Any clarification would be appreciated.


作者: B.Clark 的来源 发布者: 2017 年 12 月 27 日

回应 2


1773 作者的声誉

I will boil this down rather quickly and avoid opinion as much as possible. On some level is could be argued that there must be a procedural spine to any application.

That said, why make a class of methods rather than a file full of functions?


You can reinstantiate the same class over and over; each instance having its own state and properties. You can instantiate a class, assigned to a variable and throw it around everywhere as an object and have access to everything in it that is exposed.

There are other reasons, most to do with organizational stuff like scope, but that is the real difference.

作者: NappingRabbit 发布者: 2017 年 12 月 27 日


146 作者的声誉

I would also add that with basic OO you can have both properties and behavior within your classes and objects. That isn't possible with procedural paradigm.

作者: Fernando Mertins 发布者: 2017 年 12 月 27 日