Roger W.
Roger W.

Reputation: 337

PHP design pattern for logging

I have a class which logs a record into a file each time one of it's functions its called.

I would like to avoid including the logging code into each function. I don't believe it's a good coding practice.

class Foo {
   function foo1() {
       file_put_contents("log","Foo1 has been called");
       // do something
   }
   function foo2() {
       file_put_contents("log","Foo2 has been called");
       // do something
   }
   function foo3() {
       file_put_contents("log","Foo3 has been called";
       // do something
   }
}

I want to take out the file_put_contents() from each function but still write the log when a function is called!. Is there any design pattern to do that?

Upvotes: 1

Views: 1664

Answers (1)

chriswoodford
chriswoodford

Reputation: 1223

It sounds like you're looking for the Observer pattern.

Here's a blog post with an example that is similar enough in concept for your concerns http://labelmedia.co.uk/blog/posts/php-design-patterns-observer-pattern.html

Upvotes: 1

Related Questions