Jump to content

calling classes and objects


Trium918

Recommended Posts

<?php

class FileStorage {

	function FileStorage($path, $shoutLog = false) {
		$this->shoutLog = $shoutLog;
		$folder = 'logs';
		if (!is_dir($folder)) $folder = '../' . $folder;
		if (!is_dir($folder)) $folder = '../' . $folder;
	
		$this->path = $folder . '/' . $path . '.txt';
	}
$show_path= new FileStorage;
echo $show_path ->FileStorage();
?>

What is the correct way to call the class FileStorage's object? I am trying to test the class to see if it is working.

Edited by Trium918
Link to comment
Share on other sites

I'm assuming that's not your entire class definition. Does your complete class have a __construct() method? If not, the FileStorage() method may be used as the constructor and be called twice. Also note that the echo statement isn't going to do anything since FileStorage() doesn't return a value.

<?php

class FileStorage {

	function FileStorage($path, $shoutLog = false) {
		$this->shoutLog = $shoutLog;
		$folder = 'logs';
		if (!is_dir($folder)) $folder = '../' . $folder;
		if (!is_dir($folder)) $folder = '../' . $folder;
	
		$this->path = $folder . '/' . $path . '.txt';
	}
	
	function open($lock = false) {
		$this->handle = fopen($this->path, 'a+');

		if ($lock) {
			$this->lock();
			return $this->load();
		}
	}

	function close(&$array) {
		if (isset($array))
			$this->save($array);
				
		$this->unlock();
		fclose($this->handle);
		unset($this->handle);
	}

	function load() {
		if (($contents = $this->read($this->path)) == null)
			return $this->resetArray();

		return unserialize($contents);
	}

	function save(&$array, $unlock = true) {
		$contents = serialize($array);
		$this->write($contents);
		if ($unlock) $this->unlock();
	}

	function unlock() {
		if (isset($this->handle))
			flock($this->handle, LOCK_UN);
	}
	
	function lock() {
		if (isset($this->handle))
			flock($this->handle, LOCK_EX);
	}

	function read() {
		fseek($this->handle, 0);
		//return stream_get_contents($this->handle);
		return file_get_contents($this->path);

	}

	function write($contents) {
		ftruncate($this->handle, 0);
		fwrite($this->handle, $contents);
	}

	function resetArray() {
		if ($this->shoutLog)
			$default = array(
				'info' => array(
					'latestTimestamp' => -1
				),
	
				'posts' => array()
			);
		else
			$default = array();

		$this->save($default, false);
		return $default;
	}

}

?>

Here is the complete class. I am trying to figure out why this class isn't writing to the log file.

Link to comment
Share on other sites

The method 'FileStorage' is the constructor for the class. It has two parameters: $path and $shoutLog. The $path parameter is required. When you instantiate the class you must pass at least that first parameter. It looks like it is used as a sub-path within the folder 'logs' for storing the log files.

Link to comment
Share on other sites

The method 'FileStorage' is the constructor for the class. It has two parameters: $path and $shoutLog. The $path parameter is required. When you instantiate the class you must pass at least that first parameter. It looks like it is used as a sub-path within the folder 'logs' for storing the log files.

I am having problems writing to the file called logs.

Link to comment
Share on other sites

 

I am having problems writing to the file called logs.

 

 

And if there are problems in instantiating/setting up the class you will have problems with anything else within the class. I can't believe you are not getting errors since you are not even passing the required parameter to the class.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.