Jump to content

Parent/Child/Sibling classes without using extends?


maexus

Recommended Posts

I've been writing PHP for about 6 years now and just started to dig into the subject of OOP this week. Didn't see a huge need for it until now. Now that I have been playing with it and really falling in love with the workflow it offers, I found myself wanting to do something but didn't know how.

 

You would have a parent class. This is the object that you call in the code. You would then have children classes. They communicate with the parent and perform taskes only for that parent class. They would also be able to pass data to/from their siblings seamlessly.

 

This just makes more sense to me when it comes to structuring my logic and degating responsiblities in the code.

 

Is there a way to do this or atleast emulate it cleanly.

Link to comment
Share on other sites

To do it, no - a child can use a parent's methods (using $this->parentsmethod(); or parent::method();), but not (so far as I know) those of a child - and only if you use the child to extend the class.

 

I think it could be emulated, but it would be a workaround - you'd have to have an array in the parent containing the names of the children or something similar, and pass the names of the parent/sibling classes to the constructor of each class. I wouldn't like to be the one programming it.

 

The hardest thing for people used to procedural PHP is, in my opinion, working out how to link the classes and use them with one another. Me among them.

 

I'm currently working on the "Simplify, man" method. Break things down more and more, then call the smaller functions from a good ol' procedural script (usually a short switch statement) - meaning I never call another class from one class unless it genuinly does extend that class.

 

Eg an admin extends a user, but neither an admin or user actually extend a database (although they execute static functions of an error)

 

You have to go a bit insane and talk about your objects as people. Like the sentence above, think about them - as the name suggests - as OBJECTS. Literally.

Link to comment
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.