Reputation:
Well I'm currently working on reworking the PHP backend (sorta making my own CMS) on a a personal website for fun as well as using it as a learning opportunity.
I've been attempting to learn OOP methods in PHP, as classes seem like a great way to keep code more organized and minimize code reuse. (Though code-reuse isn't necessarily more prevalent in non-oop systems if designed correctly)
My question regards how I should handle organizing different classes:
I organized my project on a piece of paper in a form similar to this:
<!-- language: lang-php -->
<?php
class main_website{
#Has a bunch of universal configuration such as a connection to the database
function __construct($includes='webpage1.class.php'){
#will include anything listed above
}
}
class users extends main_website{
#functions relating to users
}
class webpages extends main_website{
#functions relating to all web-pages
}
class webpage1 extends webpages{
#has a set of functions that relate specifically to webpage 1 and won't be used elsewhere
}
class webpage2 extends webpages{
#has a set of functions that relate specifically to webpage 2 and won't be used elsewhere
}
?>
Now, this has been working well...ish. But I'm getting to a point where I'm creating several classes that at some point will need to communicate through each other; currently my solution is to have them communicate through their parent class, however having "sibling" classes talking to each other seems hacky so I'd like to ask if I'm understanding OOP correctly or not.
A lot of the posts on OOP are so varied and so many people have such wide views on code design and I'm not sure which ways are "right". (at least...right in terms getting my project to be somewhat "standard")
Upvotes: 0
Views: 639
Reputation: 1802
You should look to use a dependency injection container, for example Symfony's Dependency Injection component (which can be used stand-alone as explained in the article) to handle this communication.
This will make the loading of classes more efficient (utilising lazy instantiation) and also make your classes easier to unit test in isolation.
Upvotes: 2