Jump to content

Recommended Posts

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

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

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.

 

 

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.