views:

167

answers:

4

Hi All,

I'm just becoming dive into php after ages working in vb.net.

I wanna write a logger class that runs as singleon over my webapp, here the code:

class cLog{

private $data = NULL;


static private $instance = NULL;

static public function getInstance(){
    if(self::$instance == NULL){
        echo "empty!";
        self::$instance = new cLog();           
    }   
    return self::$instance;
}


private function __construct(){     
}   
private function __clone(){     
}



public function getData(){
    return self::getInstance()->data;
}

public function trace($o){
    self::getInstance()->data[] = $o;
}

}

What I expect is that, as i switch between pages of my application that make several calls to the cLog::trace() method, the data array increases and it's filled with all traces. But what I get is: everytime i run a page, the $instance is null so the object restarts (as you can see, I put an echo "empty!" line in the instance getter: it shows everytime)

I guess there's something I'm misunderstanding in the php application-lifecycle....

Here there's an usage example:

cLog::getInstance()->trace("hello world");
$logs = cLog::getInstance()->getData();

Thanks

+2  A: 

The PHP life cycle is from "page start to load" to "page ended load".

Generally speaking, every time you load a new page, everything starts from scratch.

You might be able to do some... interesting... things with session data to get your logger to work the way you want it to.

Good luck!

EToreo
A: 

But what I get is: everytime i run a page, the $instance is null so the object restarts ... I guess there's something I'm misunderstanding in the php application-lifecycle....

PHP's application-lifecycle mirrors that of HTTP. Each request for a page/URI is stateless. Each request knows nothing about the other requests. This is by design. The behavior you described is PHP acting as it should.

You need to take extra steps to have each request know about what happened in other requests. (PHP's session handling is one way to do this)

Alan Storm
+4  A: 

PHP uses a "share nothing" architecture. This means (among other things) that nothing is shared between page loads. Unlike .NET, where the application is started on the first page hit and runs until stopped, just servicing requests as they come. In PHP, every time a page is requested the application is essentially compiled and run from scratch.

Brenton Alker
A: 

Instead of addressing your question on application life cycle in PHP, I would like to make a recommendation on using a prebuilt logging class.

The Zend Framework has Zend_Log which uses the Factory pattern and has a wide variety of Writer objects to log to databases, the filesystem, email, and so forth.

http://framework.zend.com/manual/en/zend.log.writers.html

You can use Zend_Log without needing any other part of the library, so it should be easy to adapt to your current system.

Travis
It's a good suggestion, but my purpouse is to get in touch with php features. If I start copying code, I'm sure I'll find myself in troubles debugging some scripts I don't even know how they work.Maybe I'm gonna have a look and get inspired, but the real problem in my class (thus, my problem) was that I didn't understand I'm not developing in .NET anymore!
balanza