Trium918 Posted January 28, 2014 Share Posted January 28, 2014 (edited) <?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 January 28, 2014 by Trium918 Quote Link to comment Share on other sites More sharing options...
denno020 Posted January 28, 2014 Share Posted January 28, 2014 $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 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 28, 2014 Share Posted January 28, 2014 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. Quote Link to comment Share on other sites More sharing options...
Trium918 Posted January 28, 2014 Author Share Posted January 28, 2014 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. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 28, 2014 Share Posted January 28, 2014 Based on a cursory look, the class doesn't have any properties. http://www.php.net/manual/en/language.oop5.properties.php However, the class is attempting to use properties which haven't been defined. $this->shoutLog = $shoutLog; Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 28, 2014 Share Posted January 28, 2014 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. Quote Link to comment Share on other sites More sharing options...
Trium918 Posted January 29, 2014 Author Share Posted January 29, 2014 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 29, 2014 Share Posted January 29, 2014 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. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 29, 2014 Share Posted January 29, 2014 I am having problems writing to the file called logs. Have you attempted to fix the class (and the references to the class)? If so, it may help to post the updated code. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.