Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Parse error: syntax error, unexpected '$', expecting variable (T_VARIABLE) in C:\wamp\www\phpvqp3_scripts\ch08\WriteToFile.php on line 51

Posted on 2014-03-08
3
Medium Priority
?
1,038 Views
Last Modified: 2014-03-09
from larry ulman php tutorial

this file appears to be standalone

I do not know how to name a variable in line 51

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

Open in new window




Parse error: syntax error, unexpected '$', expecting variable (T_VARIABLE) in C:\wamp\www\phpvqp3_scripts\ch08\WriteToFile.php on line 51
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 1600 total points
ID: 39915472
I think that name should be $_fp: in fact, in the rest of the script the code refers to $this->_fp but the declaration of this variable is missing...
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 400 total points
ID: 39915858
This shouldn't be on two lines for a start

   private $
 = NULL;
0
 

Author Closing Comment

by:rgb192
ID: 39915909
file runs and this appears to work

the tutorial does not call the file, so i can not test

thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question