Quote:
Originally posted by DuEy
Personally I think its a joke..reusability..so when i use functions more than once im not reusing them?
|
Functions are definitely reusable but when people refer to reusability in OO they are referring to a lot more than reusable functions. You can reuse the functions themselves in regular non-OO programming but it becomes increasingly difficult to reuse the logic and methodology that those functions require to operate on the variables you pass to them, simply because the variables are not encapsulated within the functions - you have to explicitly pass them to the functions. And when the functions return variables, they put some variables into the global scope of the script that may not even need to be seen in the main logic of the script. This is where private/protected properties come into play in OO. I may have certain properties inside an object that provide information on what other functions will do with data inside that object. But since the object can operate on itself using the $this keyword, many of these variables don't even need to be seen in the main logic of the script I am using the class in. If I have 10 similar scripts operating on some data and in every case, a certain variable determines whether or not I will perform some operation on the data, it is a waste of code to have a function throw this variable out onto the global scope and add conditional statements to test if I need to perform this operation, when the operation is specific to that piece of data ONLY and shouldn't even be visible in the main logic of the code. If this data object were encapsulated in a class, this little operation could be performed from within the class and would not even be visible in the main logic of the code, which simplifies every single script that uses this class. In a large script there could be Many little conditions such as this, only making the script 10 times more complicated that necessary.