Passing object property vs passing object in php programming
In programming there is a debate on what technique is better when a function needs some input. Is it better to pass a specific property or to pass the whole object?
The latest version of php definitely supports object oriented programming. The idea behind oop is that the code is based on objects which interact with each other. In php by default objects are passed by reference, meaning that you can change the values of an object within any function, without having to change the parameters passed to it. For example if you decide that you don’t want just to get an object’s value and display it to the user, but also want to edit some of it’s values and save it, you can do it inside the function without affecting at all the rest of the code.
Using objects makes the code more readable, more extensible and easier to maintain. It’s very easy and simple to add new properties to an object just by adding them to the object’s class. Furthermore you can use many functions with the same name and the same functionality but on different objects.
Another great thing with oop is the inheritance. Inheritance lets an object to take the properties of another object and then expand in a totally different direction. This makes the code shorter, less complex and easier to understand while helping you avoid code duplication.
On performance perspective passing an object has no significant difference than passing a string. This is why when you pass an object only the pointer to the object is copied, and not all the values of each of it’s properties.
So even if there is no need for further expand of the function it is best to pass the whole object than just a specific value of it.