Help with objects in php

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:


class logger{
	
	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;	
	}
	public function write($file){
		$oldlog = file_get_contents($file);
		$post = <<<THERE
		$this->logpost;
		\

		$oldlog
THERE;
		$savedLogFile = fopen($file, 'r+');
		$lPost = fputs($savedLogFile, $post);
		fclose($savedLogFile);
	}
}

here is the test file used to impliment it:


include("log.class.php");
$me = me;
$log = new logger($me);
$log->write("log.log");

thanks

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.

Actually, $post is redundant.

You could do just

$this->logPost = <<<HERE
...
...

Here

$vars = print_r($variables, true);

You are assigning an empty variable to vars. '$variable is passed in and ‘$variables’ is assigned to $vars.

If you change it it prints the entire conents of the log.class.php file to the log file so that will need be addressed as well.

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:


class logger{
	
	public function __construct($variable, $file, $reason = NULL){
		$this->file = $file;
		$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($variable, true);
		$oldlog = file_get_contents($file);
		$post = <<<HERE
\
----------------------------------------------------------------------------------\

$reason \

IP: $ip \

Time: $today \

Array of variables collected: \

\

$vars
\
		
----------------------------------------------------------------------------------
HERE;
		$logPost = <<<THERE
		$post \

		$oldlog
THERE;
		$savedLogFile = fopen($file, 'r+');
		$lPost = fputs($savedLogFile, $logPost);
		fclose($savedLogFile);
	}
}

@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.

I know you are doing this for academics but you can also use error_log for this:
http://us2.php.net/manual/en/function.error-log.php
Example for the link:

error_log("You messed up!", 3, "my-errors.log");

If you are messing with OOP in order to learn, then how about you consider this.

Take centred effects idea, and encapsulate that in a class.

Make it so that it outputs what you have in your first example, but:

permit the user at run time to [optionally] override the server time time zone.

permit the user at run time to [optionally] override the message template

permit the user to [optionally] send in an array of variables to be printed into the log file, or just a single one.

My idea actually was to suggest how the Android logcat works - for example:


06-09 13:00:07.781: D/dalvikvm(14321): GC_CONCURRENT freed 76K, 44% free 3311K/5831K, external 518K/1030K, paused 3ms+3ms
06-09 13:00:08.007: I/global(14321): Loaded time zone names for en_US in 1308ms.
06-09 13:00:08.398: W/dalvikvm(14321): threadid=9: thread exiting with uncaught exception (group=0x40018578)
06-09 13:00:08.429: E/AndroidRuntime(14321): FATAL EXCEPTION: IntentService

It is simply - Date, Time: Level/Activity (or class/function): Message. Then you can try things like getAllLogsByLevel($level).

This code is not object orientated.

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…