drewbee Posted March 28, 2007 Share Posted March 28, 2007 Well, what I am doing and what I came here to do works... it does. However, I want to know if I am completely botching the point on OOP or not, or if I am in the right realm. Ok. I have a global functions class. It has my templating, universal functions (like random number generators, error message displays, database connections etc). It will be required on every single page that needs to load, hence the global functions class name :grin: <? class GlobalFunctions { function GlobalFunctions($pageTitle = null, $layout = 0) { } function displayErrors() { } function drawHeader() { } function drawFooter() { } } ?> Now... I have a seperate class that is used for each page individual page, this class does all the work of the specific page being accessed. Now, my question is, is that I need to call the GlobalFunctions constructor at the creation of my class below. As you can see, I simply call $this->GlobalFunctions(); To run the global functions constructor I do this: <? class MyPage extends GlobalFunctions { function MyPage($pageTitle = null, $style = 0) { $this->GlobalFunctions($pageTitle, $style); } function drawAPage() { echo "This is a page"; } } ?> So that when I create a new object for MyPage... <? $myPage = new MyPage("My Page Title"); $myPage->drawHeader(); $myPage->drawAPage(); $myPage->drawFooter(); ?> The My Page Constructor calls the GlobalFunctions constructor. Now, as I stated earlier this DOES WORK. I am here seeking advice on whether I am in the right thinking realm of using inheritance and OOP with PHP, or if I should alter it some how. Thank you for your insight, Drew Quote Link to comment https://forums.phpfreaks.com/topic/44648-solved-constructors-and-class-inheritance/ Share on other sites More sharing options...
utexas_pjm Posted March 28, 2007 Share Posted March 28, 2007 When using OOD you should try not to create a "global" anything. I would submit to you that your GlobalFunctions class is not really global in the sense that those functions (or methods) mean nothing to a Bird class or a Car class. Instead they relate to a Page specifically. With that said I would recommend changing the name of the class to Page. If you were using PHP5 I would recommend making this an abstract class. Now you can create pages that inherit the actions of your Page class. Consider the trivial example below: <?php class Page { function Page($pageTitle = null, $layout = 0) { } // this behavior is common to all pages. function displayErrors() { } // this behavior is common to all pages. function drawHeader() { } // this behavior is common to all pages. function drawFooter() { } } class LoginPage extends Page { // this behavior is unique to a login page. function processLogin() { } } class SomeContentPage extends Page { // this behavior is unique to a content page. function generateRSS() { } } ?> Hope this helps. Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/44648-solved-constructors-and-class-inheritance/#findComment-216973 Share on other sites More sharing options...
emehrkay Posted March 29, 2007 Share Posted March 29, 2007 in php5 it would be: parent::__construct(); you seem to have the general idea down Quote Link to comment https://forums.phpfreaks.com/topic/44648-solved-constructors-and-class-inheritance/#findComment-217547 Share on other sites More sharing options...
drewbee Posted March 29, 2007 Author Share Posted March 29, 2007 Cool, thanks for the information. utexas, yeah, it could be named page, I just happened to call it GlobalFunctions I guess. Everything in that class has some functions that *may* be used in sub-classes, however some may not. As such, the displayHeader/displayFooter is obviously something that will be called on each page, but their are several other functions as well like the DB connection that may or may not be initiated. I do understand what you are saying though. Thanks for the insight. Quote Link to comment https://forums.phpfreaks.com/topic/44648-solved-constructors-and-class-inheritance/#findComment-217626 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.