How to solve Parse error: parse error, expecting `T_STRING' ERROR

Parse error: parse error, expecting `T_STRING' in C:\\wamp\\www\\pmh\\lib\\PEAR\\XML\\Tree\\Node.php on line 112

Plz help me solve this Error

    /**
    * clone node and all its children (recursive)
    *
    * @return object reference to the clone-node
    */
    function &clone() {
        $clone=new XML_Tree_Node($this->name,$this->content,$this->attributes);

        $max_child=count($this->children);
        for($i=0;$i<$max_child;$i++) {
            $clone->children[]=$this->children[$i]->clone();
        }

line no. 112 is function &clone() { in above code

You’ve probably left a string open somewhere above that point in your code

Let’s read this part again carefully: “somewhere above that point in your code”. That point being line 112 function &clone(). You haven’t posted any of your code from ABOVE this point so I can only speculate on what is up there. That is why I posted a code example of the type of error you are looking for. This is why I used the word probably.

Did you actually go and look at your code to see if I was right?
If you believe line 112 is the only possible cause of the problem you obviously haven’t got much experience debugging PHP, and should be a lot less hasty to post condescending comments like “Plz dont just write whatever comes in your head”.

Good luck with it.

If you think that then point out where. As far as i know, none of string are left incomplete.Plz dont just write whatever comes in your head. Only reply if you really know the solution. Plz dont just guess.

It clearly says line 112 which is
function &clone() {

so I couldn’t understand whats wrong in this line.

If that is the case Sorry from me to Cranial-bore.

That being said, could you post the complete file?

Here is complete php file


<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | [email]license@php.net[/email] so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Bernd R&#246;mer <berndr@bonn.edu>                               |
// |          Sebastian Bergmann <sb@sebastian-bergmann.de>               |
// |          Christian K&#252;hn <ck@chkuehn.de> (escape xml entities)        |
// +----------------------------------------------------------------------+
//
// $Id: Node.php,v 1.1.1.1 2005/02/08 08:44:43 hol Exp $
//

/**
* PEAR::XML_Tree_Node
*
* @author  Bernd R&#246;mer <berndr@bonn.edu>
* @package XML_Tree
* @version 1.0  16-Aug-2001
*/
class XML_Tree_Node {
    /**
    * Attributes of this node
    *
    * @var  array
    */
    var $attributes;

    /**
    * Children of this node
    *
    * @var  array
    */
    var $children;

    /**
    * Content
    *
    * @var  string
    */
    var $content;

    /**
    * Name
    *
    * @var  string
    */
    var $name;

    /**
    * Constructor
    *
    * @param  string  name
    * @param  string  content
    * @param  array   attributes
    */
    function XML_Tree_Node($name, $content = '', $attributes = array()) {
        $this->attributes = $attributes;
        $this->children   = array();
        $this->set_content($content);
        $this->name       = $name;
    }

    /**
    * Adds a child node to this node.
    *
    * @param  mixed   child
    * @param  string  content
    * @param  array   attributes
    * @return object  reference to new child node
    */
    function &addChild($child, $content = '', $attributes = array()) {
        $index = sizeof($this->children);

        if (is_object($child)) {
            if (strtolower(get_class($child)) == 'xml_tree_node') {
                $this->children[$index] = $child;
            }

            if (strtolower(get_class($child)) == 'xml_tree' && isset($child->root)) {
                $this->children[$index] = $child->root->get_element();
            }
        } else {
            $this->children[$index] = new XML_Tree_Node($child, $content, $attributes);
        }

        return $this->children[$index];
    }

    /**
    * @deprecated
    */
    function &add_child($child, $content = '', $attributes = array()) {
        return $this->addChild($child, $content, $attributes);
    }

    /**
    * clone node and all its children (recursive)
    *
    * @return object reference to the clone-node
    */
    function &clone() {
        $clone=new XML_Tree_Node($this->name,$this->content,$this->attributes);

        $max_child=count($this->children);
        for($i=0;$i<$max_child;$i++) {
            $clone->children[]=$this->children[$i]->clone();
        }

        /* for future use....
            // clone all other vars
            $temp=get_object_vars($this);
            foreach($temp as $varname => $value)
                if (!in_array($varname,array('name','content','attributes','children')))
                    $clone->$varname=$value;
        */

        return($clone);
    }

    /**
    * inserts child ($child) to a specified child-position ($pos)
    *
    * @return  inserted node
    */
    function &insertChild($path,$pos,&$child, $content = '', $attributes = array()) {
        // direct insert of objects useing array_splice() faild :(
        array_splice($this->children,$pos,0,'dummy');
        if (is_object($child)) { // child offered is not instanziated
            // insert a single node
            if (strtolower(get_class($child)) == 'xml_tree_node') {
                $this->children[$pos]=&$child;
            }
            // insert a tree i.e insert root-element
            if (strtolower(get_class($child)) == 'xml_tree' && isset($child->root)) {
                $this->children[$pos]=$child->root->get_element();
            }
        } else { // child offered is not instanziated
            $this->children[$pos]=new XML_Tree_Node($child, $content, $attributes);
        }
        return($this);
    }

    /**
    * @deprecated
    */
    function &insert_child($path,$pos,&$child, $content = '', $attributes = array()) {
        return $this->insertChild($path,$pos,$child, $content, $attributes);
    }

    /**
    * removes child ($pos)
    *
    * @param integer pos position of child in children-list
    *
    * @return  removed node
    */
    function &removeChild($pos) {
        // array_splice() instead of a simple unset() to maintain index-integrity
        return(array_splice($this->children,$pos,1));
    }

    /**
    * @deprecated
    */
    function &remove_child($pos) {
        return $this->removeChild($pos);
    }

    /**
    * Returns text representation of this node.
    *
    * @return  string  xml
    */
    function &get()
    {
        static $deep = -1;
        static $do_ident = true;
        $deep++;
        if ($this->name !== null) {
            $ident = str_repeat('  ', $deep);
            if ($do_ident) {
                $out = $ident . '<' . $this->name;
            } else {
                $out = '<' . $this->name;
            }
            foreach ($this->attributes as $name => $value) {
                $out .= ' ' . $name . '="' . $value . '"';
            }

            $out .= '>' . $this->content;

            if (sizeof($this->children) > 0) {
                $out .= "\
";
                foreach ($this->children as $child) {
                    $out .= $child->get();
                }
            } else {
                $ident = '';
            }
            if ($do_ident) {
                $out .= $ident . '</' . $this->name . ">\
";
            } else {
                $out .= '</' . $this->name . '>';
            }
            $do_ident = true;
        } else {
            $out = $this->content;
            $do_ident = false;
        }
        $deep--;
        return $out;
    }

    /**
    * Gets an attribute by its name.
    *
    * @param  string  name
    * @return string  attribute
    */
    function getAttribute($name) {
        return $this->attributes[strtolower($name)];
    }

    /**
    * @deprecated
    */
    function get_attribute($name) {
        return $this->getAttribute($name);
    }

    /**
    * Gets an element by its 'path'.
    *
    * @param  string  path
    * @return object  element
    */
    function &getElement($path) {
        if (sizeof($path) == 0) {
            return $this;
        }

        $next = array_shift($path);

        return $this->children[$next]->get_element($path);
    }

    /**
    * @deprecated
    */
    function &get_element($path) {
        return $this->getElement($path);
    }

    /**
    * Sets an attribute.
    *
    * @param  string  name
    * @param  string  value
    */
    function setAttribute($name, $value = '') {
        $this->attributes[strtolower($name)] = $value;
    }

    /**
    * @deprecated
    */
    function set_attribute($name, $value = '') {
        return $this->setAttribute($name, $value);
    }

    /**
    * Unsets an attribute.
    *
    * @param  string  name
    */
    function unsetAttribute($name) {
        unset($this->attributes[strtolower($name)]);
    }

    /**
    * @deprecated
    */
    function unset_attribute($name) {
        return $this->unsetAttribute($name);
    }

    /**
    *
    *
    */
    function setContent(&$content)
    {
        $this->content = $this->_xml_entities($content);
    }

    function set_content(&$content)
    {
        return $this->setContent($content);
    }

    /**
    * Escape XML entities.
    *
    * @param   string  xml
    * @return  string  xml
    * @access  private
    */
    function _xml_entities($xml) {
        $xml = str_replace(array('&#252;', '&#220;', '&#246;',
                                 '&#214;', '&#228;', '&#196;',
                                 '&#223;'
                                ),
                           array('&#252;', '&#220;', '&#246;',
                                 '&#214;', '&#228;', '&#196;',
                                 '&#223;'
                                ),
                           $xml
                          );

        $xml = preg_replace(array("/\\&([a-z\\d\\#]+)\\;/i",
                                  "/\\&/",
                                  "/\\#\\|\\|([a-z\\d\\#]+)\\|\\|\\#/i",
                                  "/([^a-zA-Z\\d\\s\\<\\>\\&\\;\\.\\:\\=\\"\\-\\/\\&#37;\\?\\!\\'\\(\\)\\[\\]\\{\\}\\$\\#\\+\\,\\@_])/e"
                                 ),
                            array("#||\\\\1||#",
                                  "&amp;",
                                  "&\\\\1;",
                                  "'&#'.ord('\\\\1').';'"
                                 ),
                            $xml
                           );

        return $xml;
    }

    /**
    * Print text representation of XML tree.
    */
    function dump() {
        echo $this->get();
    }
}
?>


I am trying to work on it to make it working for new php version

He can’t point that out as it isn’t in the code you’ve posted. Your function seems fine, so what cranial-bore suggests that the error may be before the code you posted may indeed be the case.
When PHP indicates an error on line this-and-that, it may be before that line, but the interpreter doesn’t choke on the line that actually contains the error, but on the next line.

That being said, could you post the complete file?

PS. You can use [noparse]


[/noparse] tags to get PHP color highlighted code, instead of using [noparse]


[/noparse], which has no syntax highlighting

It seems PHP doesn’t like it when you name a function “clone”, as it’s a reserved keyword.
When I renamed “clone” to “xclone” the code worked fine.

You’ve probably left a string open somewhere above that point in your code (or forgotten a ; to end a statement)

e.g.


$something = 'This is a string that never ends


/**
* clone node and all its children (recursive)
*
* @return object reference to the clone-node
*/
function &clone() {
    $clone=new XML_Tree_Node($this->name,$this->content,$this->attributes);

    $max_child=count($this->children);
    for($i=0;$i<$max_child;$i++) {
        $clone->children[]=$this->children[$i]->clone();
    }