venturemc Posted September 13, 2009 Share Posted September 13, 2009 Although I've studided OOP in a previous life, I'm a little rusty on the concepts. I'm very comfortable with procedural programming, but am forcing myself to get comfortable with OOP as well. I've managed to build and implement a few simple classes, but in my current self-imposed exercise, I'm tripping myself up a little. I'm not sure if it's a poor design idea, or PHP specific struggles. What I'm trying to do is build a parent/child relationship between several classes. Let's say that I'm building an 'office' class. The office has several independent objects like 'bookcase', 'desk', 'chair', 'lamp' etc each with their own set of properties. The office class itself does little but act as an interface to the child classes. This could easily be done all within one class, but it seems like it makes sense to break the class into several child classes that all accessed by interfcing with the parent. Bad programming? I've tried using the includes statement in my parent class, which seems fine as long as it's declared before the class declaration. <? PHP include 'my_stuff.php'; include 'desk.cls.php'; //etc class office{ } ?> The problem I'm encountering is when I try to create a new child object. <? PHP include 'my_stuff.php'; include 'desk.cls.php'; //etc class office{ var $off_desk; $offdesk1 = new desk; //etc.... } ?> I don't know if it's just the editor I'm using (Eclipse) or if I'm doing something wrong. I keep getting an "incomplete class declaration" error (in the editor) when I try to create a new child. I have successfully used another "includes" library within the parent, but it's just procedural stuff - not a class. Is this the approach (procedural) I should take with my child objects? Seems to defeat the purpose. I'd appreciate some schooling on this concept and would like to know how others are dealing with this type of issue. Quote Link to comment https://forums.phpfreaks.com/topic/174046-classes-oop-etc/ Share on other sites More sharing options...
ngreenwood6 Posted September 13, 2009 Share Posted September 13, 2009 one problem i see is that you arent instantiating(creating) the desk object corretly. change this line: $offdesk1 = new desk; to this: $offdesk1 = new desk(); That might correct the error you are getting. Quote Link to comment https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917479 Share on other sites More sharing options...
venturemc Posted September 13, 2009 Author Share Posted September 13, 2009 While at first, I dismissed your concept as 'just a typo' on my part (the 'code' posted is just an example of what I'm trying to do), I did indeed find that my syntax was wrong in my actual code (ironic, huh?). Alas, adding the () didn't fix my issue. :'( Quote Link to comment https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917483 Share on other sites More sharing options...
ngreenwood6 Posted September 13, 2009 Share Posted September 13, 2009 without seeing your REAL code it is going to be hard to troubleshot. If you would be so kind to post it I might be able to help you further. Quote Link to comment https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917485 Share on other sites More sharing options...
PrinceOfDragons Posted September 13, 2009 Share Posted September 13, 2009 The best tutorial on PHP OOP that I have seen is at http://devzone.zend.com/article/638-PHP-101-part-7-The-Bear-Necessities. Just read over it slowly and practice it helped me out a lot. Quote Link to comment https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917493 Share on other sites More sharing options...
venturemc Posted September 13, 2009 Author Share Posted September 13, 2009 THanks. That is a good read and I agree and one of the clearest complete examples of OOOP in PHP that I've seen. But it doesn't cover what I'm looking to do nor does it address the programming concept of how to best organize a bunch of code in a class. I could easily do all that I want with my child objects within the parent class with one large script either with subclasses or just functions. I was thinking that it would make more sense to break up the parent object into several class scripts to make it all more manageble. Maybe I need a large script here rather than several smaller ones? But I kind of get the impression that OOP is designed to be able to cut down on big expansive scripts. Let's say I 'm building a 'car' class. The car class would contain several objects: 'motor', 'tires', 'dashboard', 'upholstery', etc. Each of these objects have unique properties of their own. It seems like I should be able to build a car class, that interacts with each of it's objects. Again, I could either do it all in one huge script, or I could even just access each 'child' object directly from a php script that accesses the 'clidren' and not directly associate the parent 'car' class to the child objectsl. Either of these methods seems to defeat the purpose of classes though. Perhaps I'm just trying to make it too fancy? Quote Link to comment https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917694 Share on other sites More sharing options...
wshell Posted September 13, 2009 Share Posted September 13, 2009 I could easily do all that I want with my child objects within the parent class with one large script either with subclasses or just functions. I was thinking that it would make more sense to break up the parent object into several class scripts to make it all more manageble. You are absolutely correct! OOP is not only a style but a way of organizing code. Keeping things manageable is key. Your code, after adding the () when you Instantiate your object, looks correct. This leads me to believe the problem is in your class that you are trying to instantiate. Everything you are doing, and your thought process for designing your application is all sound. Post the code in your class file and lets see if we can figure this out! LIVE, LEARN, SHARE - ws Quote Link to comment https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917731 Share on other sites More sharing options...
Philip Posted September 13, 2009 Share Posted September 13, 2009 Adding () won't fix the problem as they are optional when there aren't any parameters required. However, if in your desk class you have a construct function that requires parameters - you'd get that error. Quote Link to comment https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917733 Share on other sites More sharing options...
venturemc Posted September 13, 2009 Author Share Posted September 13, 2009 So if I'm understanding everyone correctly, I should be able to use other classes inside of a class? My concept isn't wrong, just have a syntax issue somewhere? Quote Link to comment https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917790 Share on other sites More sharing options...
wshell Posted September 13, 2009 Share Posted September 13, 2009 yup.. that is correct. Quote Link to comment https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917793 Share on other sites More sharing options...
venturemc Posted September 13, 2009 Author Share Posted September 13, 2009 thanks much! Quote Link to comment https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917817 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.