Jump to content
Larry Ullman's Book Forums

Recommended Posts

I'm finding it difficult to view errors and I'm wondering if it's due to OOP interface. I didn't seem to have this problem with procedural programming. The error I seem to be seeing often is 500 Internal Server Error and sometimes it turns out to be a simple syntax error. I've been using this script in the code but it doesn't work consistently:

 

 

// Display errors
ini_set('display_errors', 1);
 
// Full error reporting
 error_reporting(E_ALL);
 
I'm using a hosting service that has PHP 5.3.13 installed. Any suggestions?
 
Also,
Script 8.3 demonstrates the idea of extending the Exception class. To provide a more detailed list of errors, a class extending the Exception class is created and the getDetails method is defined to account for common errors. What I don't understand is how the method is called. In the attendant class WriteToFile, conditions are evaluated and exceptions are thrown to the FileException class but not the getDetails function specifically.
 
if (empty($file)) throw new FileException($this->_message, 0);
 
Also, the conditional operator in the switch statement references a variable but I don't see it declared anywhere.
 
switch ($this->code)
 
here is the entire class definition:
 
<?php # Script 8.3 - WriteToFile2.php
// This page defines a WriteToFile and a FileException class.
 
/* The FileException class.
 * The class creates one new method: getDetails().
 */
class FileException extends Exception {
 
    // For returning more detailed error messages:
    function getDetails() {
 
        // Return a different message based upon the code:
        switch ($this->code) {
            case 0:
                return 'No filename was provided';
                break;
            case 1:
                return 'The file does not exist.';
                break;
            case 2:
                return 'The file is not a file.';
                break;
            case 3:
                return 'The file is not writable.';
                break;
            case 4:
                return 'An invalid mode was provided.';
                break;
            case 5:
                return 'The data could not be written.';
                break;
            case 6:
                return 'The file could not be closed.';
                break;
            default:
                return 'No further information is available.';
                break;
        } // End of SWITCH.
    
    } // End of getDetails() method.
    
} // End of FileException class.
 
/* The WriteToFile class.
 * The class contains one attribute: $_fp.
 * The class contains three methods: __construct(), write(), close(), and __destruct().
 */
class WriteToFile {
    
    // For storing the file pointer:
    private $
 = NULL;
 
    // For storing an error message:
    private $_message = '';
    
    // Constructor opens the file:
    function __construct($file = null, $mode = 'w') {
    
        // Assign the file name and mode
        // to the message attribute:
        $this->_message = "File: $file Mode: $mode";
        
        // Make sure a file name was provided:
        if (empty($file)) throw new FileException($this->_message, 0);
 
        // Make sure the file exists:
        if (!file_exists($file)) throw new FileException($this->_message, 1);
 
        // Make sure the file is a file:
        if (!is_file($file)) throw new FileException($this->_message, 2);
 
        // Make sure the file is writable, when necessary
        if (!is_writable($file)) throw new FileException($this->_message, 3);
 
        // Validate the mode:
        if (!in_array($mode, array('a', 'a+', 'w', 'w+'))) throw new FileException($this->_message, 4);
                
        // Open the file:
        $this->_fp = fopen($file, $mode);
 
    } // End of constructor.
    
    // This method writes data to the file:
    function write($data) {
 
        // Confirm the write:
        if (@!fwrite($this->_fp, $data . "\n")) throw new FileException($this->_message . " Data: $data", 5);
 
    } // End of write() method.
    
    // This method closes the file:
    function close() {
 
        // Make sure it's open:
        if ($this->_fp) {
            if (@!fclose($this->_fp)) throw new FileException($this->_message, 6);
            $this->_fp = NULL;
        }   
 
    } // End of close() method.
 
    // The destructor calls close(), just in case:
    function __destruct() {
        $this->close();
    } // End of destructor.
    
} // End of WriteToFile class.
 
Regarding the section on the Autoloader function, (script 8.10) I realized that the class name must be identical to the file name in order for the function to properly work. I originally had the ShapeFactory class within the shapefactory file and received no errors when initially run using the standard require statement.
 
Link to comment
Share on other sites

Yes, I have other scripts in the same directory that run once the errors are corrected. It hasn't prevented me from resolving runtime issues because sometimes I'll place the error detection script into the class definition script and get some sort of confirmation that the origin of the error is somewhere in the class definition. But the main calling script, when run, will at times just issue the 'Internal Server Error' message without any indication of the source of the error. I suppose I could check the server error log but I just thought I'd put it out there. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...