black.horizons Posted February 9, 2009 Share Posted February 9, 2009 Hi, I'm writing an application using OOP. At the minute the majority of my functions that return a large amount of information do so using an array. I'm wondering, should I be using objects though? TIA, Alex Quote Link to comment https://forums.phpfreaks.com/topic/144510-solved-oop-return-object-or-array/ Share on other sites More sharing options...
Maq Posted February 9, 2009 Share Posted February 9, 2009 I'm wondering, should I be using objects though? I thought you said you were using OOP... I'm writing an application using OOP. Your class should contain the methods. To get to those methods you should create an object of that class. Is that how you're doing it? Or am I misunderstanding the question? Quote Link to comment https://forums.phpfreaks.com/topic/144510-solved-oop-return-object-or-array/#findComment-758316 Share on other sites More sharing options...
.josh Posted February 9, 2009 Share Posted February 9, 2009 no...if you are thinking of an object as just a fancy variable then I think you might be missing the point of oop... Quote Link to comment https://forums.phpfreaks.com/topic/144510-solved-oop-return-object-or-array/#findComment-758317 Share on other sites More sharing options...
gizmola Posted February 9, 2009 Share Posted February 9, 2009 One consideration for this is what version of PHP you are using. If you're using php5 then all objects are passed by reference. In PHP4 this was not the case. Hopefully you're using PHP5. If it's appropriate for your application to be dealing with objects then you should be using them. Only you can answer that question. For example, if you have a factory class that creates objects, then it would be a very poor design if it did not in fact pass back the object it had manufactured. Quote Link to comment https://forums.phpfreaks.com/topic/144510-solved-oop-return-object-or-array/#findComment-758321 Share on other sites More sharing options...
black.horizons Posted February 9, 2009 Author Share Posted February 9, 2009 i'm using PHP5. I have an organisation class which has several methods each returning an array of data that is selected from a database and parsed (several calculations are made on the returned data). should i be using objects instead? Quote Link to comment https://forums.phpfreaks.com/topic/144510-solved-oop-return-object-or-array/#findComment-758532 Share on other sites More sharing options...
trq Posted February 9, 2009 Share Posted February 9, 2009 i'm using PHP5. I have an organisation class which has several methods each returning an array of data that is selected from a database and parsed (several calculations are made on the returned data). should i be using objects instead? If all you would be using these objects for is to store data (ie; the objects themselves would consist of only properties and no methods) then I don't see why arrays wouldn't be the prefered option. Quote Link to comment https://forums.phpfreaks.com/topic/144510-solved-oop-return-object-or-array/#findComment-758537 Share on other sites More sharing options...
coder_ Posted February 9, 2009 Share Posted February 9, 2009 Well, whatever is the easiest way for you. For better data handling in application you could use array. Whatever you like. Quote Link to comment https://forums.phpfreaks.com/topic/144510-solved-oop-return-object-or-array/#findComment-758538 Share on other sites More sharing options...
black.horizons Posted February 9, 2009 Author Share Posted February 9, 2009 i think i'll stick with arrays then! i appreciate the help - think i'll mark this one solved! Quote Link to comment https://forums.phpfreaks.com/topic/144510-solved-oop-return-object-or-array/#findComment-758544 Share on other sites More sharing options...
gizmola Posted April 21, 2009 Share Posted April 21, 2009 As devil's advocate, one of the goals of oop is "information hiding". The idea is that the class should hide the details of the internal data structures. Passing giant arrays around from method to method is a procedural approach. I'm not saying that you should change that approach just for the sake of an OOP principle, but if the arrays are large and complicated, it could in fact be a more modular approach to instead instantiate objects that have methods which return the results of computations done on the internal data, as it's needed, with all the internal querying and fetching being private to appropriate object classes. The idea is that, if you need to change the internal data structures, you don't have a whole host of functions that are suddenly broken because the structure of the data no longer matches what they were working with. Quote Link to comment https://forums.phpfreaks.com/topic/144510-solved-oop-return-object-or-array/#findComment-815713 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.