I have a class with only one function:
<?php
class EventLog
{
public function logEvent($data, $object, $operation, $id)
{
//Log it to a file...
$currentTime = new DateTime();
$time = $currentTime->format('Y-m-d H:i:s');
$logFile = "/.../event_log.txt";
$message = "Hello world";
//Send the data to a file...
file_put_contents($logFile, $message, FILE_APPEND);
}
}
Then I have another class with many functions and each and everyone need to call the above method. To instantiate the class in every function I have done:
$log = new EventLog();
//Then...
$log->logEvent($data, $object, $operation, $id);
The problem: I have used the above code in every function and what I would like to know is if there is a way to instantiate the EventLog class once for all the functions that need it.