1

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?

2
  • Traits! php.net/manual/en/language.oop5.traits.php Commented Jan 29, 2013 at 6:13
  • 1
    The PHP Framework Interopability Group recently formalised a standard design pattern for writing logging classes. You might be interested in having a look -- phpdeveloper.org/news/19037 Commented Jan 29, 2013 at 9:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.