Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/174046-classes-oop-etc/
Share on other sites

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. :'(

Link to comment
https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917483
Share on other sites

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?

 

Link to comment
https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917694
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/174046-classes-oop-etc/#findComment-917731
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.