Jump to content

Recommended Posts

I'm somewhat new to the world of OOP PHP but have most of the basics down.  I'm currently working to create a basic debug log class/structure to use when building new applications.  What I'd like the end result to be is to have all the classes that are used to build a page (ex: Users, Database, Modules, etc...) all use the same instance of the debug class to print a nice log at the end of execution.

 

My first attempt at this has been to create a GenericClass and have the Debug, Database, and  Module classes all extend it.  In the GenericClass __constructor(), I would create a new instance of the Debug class and set that to $this->debug.

 

The problem I'm running into is the Database and Module classes are not inheriting the same Debug instance.  I assume that it is because each time I call new Database - the GenericClass is instantiated again, and therefore the Debug class is as well.

 

So the question is, how do I "automatically" pass the same instance of the debug class to all other classes being used?  Preferably without setting a global variable and passing it to each class. 

 

Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/
Share on other sites

You'll be needing a singleton.

 

<?php

class Debug {

  private static $instance;

  private function __construct() {}

  public static function getInstance() {
    if (!isset(self::$instance)) {
      $obj = __CLASS__;
      self::$instance = new $obj;
    }
    return self::$instance;
  }

  // other methods.

}

$Debug = Debug::getInstance();

?>

Thorpe, quick question.  I've seen people, when making a new singleton, use

$obj = __CLASS__;
self::$instance = new $obj;

 

And

self::$instance = new self;

 

Any difference? I always use the latter.

 

I wouldn't know. I would have thought the later would have simply returned 'Object' or something simular? while __CLASS__ returns the name of the class.

So, just to make sure I understand this right - I would then instantiate the Debug class in GeneralClass __constructor? 

 

class GeneralClass
{
    var $debug;

    function __constructor()
    {
          $this->debug =  Debug::getInstance();
    }
}

 

That would give me the same instance of the debug class for any class that extends GeneralClass, correct?

Now that I've had time to think about this - if creating a overall generic class that all other classes extend is not a good idea, is the best way to get the debug instance to each class just creating a $debug var in each class and set the value to the $Debug var?  I can't think of any other way to "automatically" achieve this...

 

 

Hi kmfstudios,

 

I think that is the best way to do it if you need the class to be instantiate the debug class.

 

Have you considered making your debug methods entirely static?

 

It seems to me that a log class would not need an instance, you could just call the log functions like

 

Debug::LogError($message);

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.