Hey everyone i’m new to object oriented programming in php, so to get more affiliated with it, i decided to create a logger class which would be a quick way to create a log of specific variables, however when i attempt to use it the log files just shows a ‘;’ and a bunch of empty lines, here is my php:
I’ve had only a quick look at your code but the first thing you need is to assign properties/attributes for each instance of the object/class. An object has properties and methods/functions to manipulate those properties.
So, you will need something like this
class logger{
protected $logPost; // **** this is a property of the object ****
public function __construct($variable){
$ip = getenv("REMOTE_ADDR");
date_default_timezone_set("America/New_York");
$time = $_SERVER['REQUEST_TIME'];
$today = date('Y-m-d H:i:s', $time);
$vars = print_r($variables, true);
$post = <<<HERE
----------------------------------------------------------------------------------\
IP: $ip \
Time: $today \
\
Array of variables collected: \
\
$vars
\
----------------------------------------------------------------------------------\
HERE;
$this->logPost = $post;
}
...
...
then each instance of the logger object will have its own $logPost property.
thanks JeremyC, that was a really dumb mistake, and thanks kennard for helping me with the logic, i made a couple edits based off this, my final working code is:
@maxdream01 - you can probably wrap that in a regluar function at that point - No need to make it a class. You also still may have an error as you are not noting the property $this->file.
Care to elaborate? It may not be a best example of OOP but it is also not the worst I have ever seen. The constructor is contains a lot of functionality that could/should be delegated to methods but it still functions as an object…