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.

Link to comment
https://forums.phpfreaks.com/topic/285740-calling-classes-and-objects/
Share on other sites

 

$show_path = new FileStorage();
echo $show_path->FileStorage($path);

 

That should work.. Make sure you do it outside the class definition, or inside another function within that class that is called externally.

 

Hopefully that helps

Denno

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.

  On 1/28/2014 at 1:36 PM, cyberRobot said:

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.

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.

  On 1/28/2014 at 3:24 PM, Psycho said:

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.

 

  On 1/29/2014 at 12:36 PM, Trium918 said:

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.