PHP: "Cannot redeclare class ..." even if I change the name of the class
<p>I have downloaded the <a href="https://cms.paypal.com/cms_content/US/en_US/files/developer/PP_PHP_SOAP_SDK.zip" rel="nofollow">PayPal API (SOAP) for PHP</a>.</p>

<p>I'm getting this error:</p>

<blockquote>
  <p>Fatal error: Cannot redeclare class
  SOAP_Client in
  /home/me/app1/plugins/mbpPlatformFrontendPlugin/lib/paypal/PayPal/SOAP/Client.php
  on line 83</p>
</blockquote>

<p><strong>The strange thing is:</strong> if I change the name of the class to what ever else, for example SOAP_Client_foo, I get </p>

<blockquote>
  <p>Fatal error: Cannot redeclare class
  SOAP_Client_foo in
  /home/me/app1/plugins/mbpPlatformFrontendPlugin/lib/paypal/PayPal/SOAP/Client.php
  on line 83</p>
</blockquote>

<p>Any idea?</p>

<pre><code>&lt;?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 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          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Shane Caraveo &lt;Shane@Caraveo.com&gt;   Port to PEAR and more   |
// | Authors: Dietrich Ayala &lt;dietrich@ganx4.com&gt; Original Author         |
// +----------------------------------------------------------------------+
//
// $Id: Client.php,v 1.1.1.1 2006/02/19 08:15:20 dennis Exp $
//

//require_once 'PayPal/SOAP/Value.php';
//require_once 'PayPal/SOAP/Base.php';
//require_once 'PayPal/SOAP/Transport.php';
//require_once 'PayPal/SOAP/WSDL.php';
//require_once 'PayPal/SOAP/Fault.php';
//require_once 'PayPal/SOAP/Parser.php';
//require_once 'PayPal/SOAP/PatternReplace.php';

// Arnaud: the following code was taken from DataObject
// and adapted to suit

// this will be horrifically slow!!!!
// NOTE: Overload SEGFAULTS ON PHP4 + Zend Optimizer
// these two are BC/FC handlers for call in PHP4/5

if (!class_exists('SOAP_Client_Overload')) {
    if (substr(phpversion(), 0, 1) == 5) {
        class SOAP_Client_Overload extends SOAP_Base
        {
            function __call($method, $args)
            {
                $return = null;
                $this-&gt;_call($method, $args, $return);
                return $return;
            }
        }
    } else {
        if (!function_exists('clone')) {
            eval('function clone($t) { return $t; }');
        }
        eval('
            class SOAP_Client_Overload extends SOAP_Base
            {
                function __call($method, $args, &amp;$return)
                {
                    return $this-&gt;_call($method, $args, $return);
                }
            }');
    }
}

/**
 * SOAP Client Class
 *
 * This class is the main interface for making soap requests.
 *
 * basic usage:
 *   $soapclient = new SOAP_Client( string path [ , boolean wsdl] );
 *   echo $soapclient-&gt;call( string methodname [ , array parameters] );
 *
 * Originally based on SOAPx4 by Dietrich Ayala
 * http://dietrich.ganx4.com/soapx4
 *
 * @access   public
 * @package  SOAP::Client
 * @author   Shane Caraveo &lt;shane@php.net&gt; Conversion to PEAR and updates
 * @author   Stig Bakken &lt;ssb@fast.no&gt; Conversion to PEAR
 * @author   Dietrich Ayala &lt;dietrich@ganx4.com&gt; Original Author
 */
class SOAP_Client extends SOAP_Client_Overload
{
    /**
     * Communication endpoint.
     *
     * Currently the following transport formats are supported:
     *  - HTTP
     *  - SMTP
     *
     * Example endpoints:
     *   http://www.example.com/soap/server.php
     *   https://www.example.com/soap/server.php
     *   mailto:soap@example.com
     *
     * @var  string
     * @see  SOAP_Client()
     */
    public $_endpoint = '';

    /**
     * portname
     *
     * @var string contains the SOAP PORT name that is used by the client
     */
    public $_portName = '';

    /**
     * Endpoint type
     *
     * @var  string  e.g. wdsl
     */
    public $__endpointType = '';

    /**
     * wire
     *
     * @var  string  contains outoing and incoming data stream for debugging.
     */
    public $xml; // contains the received xml
    public $wire;
    public $__last_request = null;
    public $__last_response = null;

    /**
     * Options
     *
     * @var array
     */
    public $__options = array('trace'=&gt;0);

    /**
     * encoding
     *
     * @var  string  Contains the character encoding used for XML parser, etc.
     */
    public $_encoding = SOAP_DEFAULT_ENCODING;

    /**
     * headersOut
     *
     * @var  array  contains an array of SOAP_Headers that we are sending
     */
    public $headersOut = null;

    /**
     * headersOut
     *
     * @var  array  contains an array headers we recieved back in the response
     */
    public $headersIn = null;

    /**
     * __proxy_params
     *
     * @var  array  contains options for HTTP_Request class (see HTTP/Request.php)
     */
    public $__proxy_params = array();

    public $_soap_transport = null;

    /**
     * SOAP_Client constructor
     *
     * @param string endpoint (URL)
     * @param boolean wsdl (true if endpoint is a wsdl file)
     * @param string portName
     * @param array  contains options for HTTP_Request class (see HTTP/Request.php)
     * @access public
     */
    function SOAP_Client($endpoint, $wsdl = false, $portName = false, $proxy_params=array())
    {
        parent::SOAP_Base('Client');
        $this-&gt;_endpoint = $endpoint;
        $this-&gt;_portName = $portName;
        $this-&gt;__proxy_params = $proxy_params;

        $wsdl = $wsdl ? $wsdl : strcasecmp('wsdl', substr($endpoint, strlen($endpoint) - 4)) == 0;

        // make values
        if ($wsdl) {
            $this-&gt;__endpointType = 'wsdl';
            // instantiate wsdl class
            $this-&gt;_wsdl =  new SOAP_WSDL($this-&gt;_endpoint, $this-&gt;__proxy_params);
            if ($this-&gt;_wsdl-&gt;fault) {
                $this-&gt;_raiseSoapFault($this-&gt;_wsdl-&gt;fault);
            }
        }
    }

    function _reset()
    {
        $this-&gt;xml = null;
        $this-&gt;wire = null;
        $this-&gt;__last_request = null;
        $this-&gt;__last_response = null;
        $this-&gt;headersIn = null;
        $this-&gt;headersOut = null;
    }

    /**
     * setEncoding
     *
     * set the character encoding, limited to 'UTF-8', 'US_ASCII' and 'ISO-8859-1'
     *
     * @param string encoding
     * @return mixed returns null or SOAP_Fault
     * @access public
     */
    function setEncoding($encoding)
    {
        if (in_array($encoding, $this-&gt;_encodings)) {
            $this-&gt;_encoding = $encoding;
            return null;
        }
        return $this-&gt;_raiseSoapFault('Invalid Encoding');
    }

    /**
     * addHeader
     *
     * To add headers to the envelop, you use this function, sending it a
     * SOAP_Header class instance.
     *
     * @param SOAP_Header a soap value to send as a header
     * @access public
     */
    function addHeader(&amp;$soap_value)
    {
        # add a new header to the message
        if (is_a($soap_value,'soap_header')) {
            $this-&gt;headersOut[] =&amp; $soap_value;
        } else if (gettype($soap_value) == 'array') {
            // name, value, namespace, mustunderstand, actor
            $this-&gt;headersOut[] =  new SOAP_Header($soap_value[0], null, $soap_value[1], $soap_value[2], $soap_value[3]);;
        } else {
            $this-&gt;_raiseSoapFault("Don't understand the header info you provided.  Must be array or SOAP_Header.");
        }
    }

    /**
     * SOAP_Client::call
     *
     * the namespace parameter is overloaded to accept an array of
     * options that can contain data necessary for various transports
     * if it is used as an array, it MAY contain a namespace value and a
     * soapaction value.  If it is overloaded, the soapaction parameter is
     * ignored and MUST be placed in the options array.  This is done
     * to provide backwards compatibility with current clients, but
     * may be removed in the future.
     *
     * @param string method
     * @param array  params
     * @param array options (hash with namespace, soapaction, timeout, from, subject, etc.)
     *
     * The options parameter can have a variety of values added.  The currently supported
     * values are:
     *   namespace
     *   soapaction
     *   timeout (http socket timeout)
     *   from (smtp)
     *   transfer-encoding (smtp, sets the Content-Transfer-Encoding header)
     *   subject (smtp, subject header)
     *   headers (smtp, array-hash of extra smtp headers)
     *
     * @return array of results
     * @access public
     */
    function &amp;call($method, &amp;$params, $namespace = false, $soapAction = false)
    {
        $this-&gt;headersIn = null;
        $this-&gt;__last_request = null;
        $this-&gt;__last_response = null;
        $this-&gt;wire = null;
        $this-&gt;xml = null;

        $soap_data =&amp; $this-&gt;__generate($method, $params, $namespace, $soapAction);
        if (PEAR::isError($soap_data)) {
            return $this-&gt;_raiseSoapFault($soap_data);
        }

        // __generate may have changed the endpoint if the wsdl has more
        // than one service, so we need to see if we need to generate
        // a new transport to hook to a different URI.  Since the transport
        // protocol can also change, we need to get an entirely new object,
        // though this could probably be optimized.
        if (!$this-&gt;_soap_transport || $this-&gt;_endpoint != $this-&gt;_soap_transport-&gt;url) {
            $this-&gt;_soap_transport =&amp; SOAP_Transport::getTransport($this-&gt;_endpoint);
            if (PEAR::isError($this-&gt;_soap_transport)) {
                $fault =&amp; $this-&gt;_soap_transport;
                $this-&gt;_soap_transport = null;
                return $this-&gt;_raiseSoapFault($fault);
            }
        }
        $this-&gt;_soap_transport-&gt;encoding = $this-&gt;_encoding;

        // Send the message.
        $transport_options = array_merge_recursive($this-&gt;__proxy_params, $this-&gt;__options);

      // This is to replace "_" by "-" since wsdl has "-" and not "_"   (see NOTE in Readme.html)
        if(file_exists(dirname(__FILE__) . '/PatternReplace.php')){
                 $soap_data =  underscore_replace($soap_data);
        }

        $this-&gt;xml =&amp; $this-&gt;_soap_transport-&gt;send($soap_data, $transport_options);

        // Save the wire information for debugging.
        if ($this-&gt;__options['trace'] &gt; 0) {
            $this-&gt;__last_request =&amp; $this-&gt;_soap_transport-&gt;outgoing_payload;
            $this-&gt;__last_response =&amp; $this-&gt;_soap_transport-&gt;incoming_payload;
            $this-&gt;wire =&amp; $this-&gt;__get_wire();
        }
        if ($this-&gt;_soap_transport-&gt;fault) {
            return $this-&gt;_raiseSoapFault($this-&gt;xml);
        }

        $this-&gt;__attachments =&amp; $this-&gt;_soap_transport-&gt;attachments;
        $this-&gt;__result_encoding = $this-&gt;_soap_transport-&gt;result_encoding;

        if (isset($this-&gt;__options['result']) &amp;&amp; $this-&gt;__options['result'] != 'parse') {
            return $this-&gt;xml;
        }

        return $this-&gt;__parse($this-&gt;xml, $this-&gt;__result_encoding, $this-&gt;__attachments);
    }

    /**
     * Sets option to use with the transports layers.
     *
     * An example of such use is
     * $soapclient-&gt;setOpt('curl', CURLOPT_VERBOSE, 1)
     * to pass a specific option to when using an SSL connection.
     *
     * @access public
     * @param  string  $category  category to which the option applies
     * @param  string  $option    option name
     * @param  string  $value     option value
     * @return void
     */
    function setOpt($category, $option, $value = null)
    {
        if (!is_null($value)) {
            if (!isset($this-&gt;__options[$category])) {
                $this-&gt;__options[$category] = array();
            }
            $this-&gt;__options[$category][$option] = $value;
        } else {
            $this-&gt;__options[$category] = $option;
        }
    }

    /**
     * Overload extension support
     * if the overload extension is loaded, you can call the client class
     * with a soap method name
     * $soap = new SOAP_Client(....);
     * $value = $soap-&gt;getStockQuote('MSFT');
     *
     * @param string method
     * @param array  args
     * @param string retur_value
     *
     * @return boolean
     * @access public
     */
    function _call($method, $args, &amp;$return_value)
    {
        // XXX overloading lowercases the method name, we
        // need to look into the wsdl and try to find
        // the correct method name to get the correct
        // case for the call.
        if ($this-&gt;_wsdl) {
            $this-&gt;_wsdl-&gt;matchMethod($method);
        }

        $return_value =&amp; $this-&gt;call($method, $args);
        return true;
    }

    function &amp;__getlastrequest()
    {
        return $this-&gt;__last_request;
    }

    function &amp;__getlastresponse()
    {
        return $this-&gt;__last_response;
    }

    function __use($use)
    {
        $this-&gt;__options['use'] = $use;
    }

    function __style($style)
    {
        $this-&gt;__options['style'] = $style;
    }

    function __trace($level)
    {
        $this-&gt;__options['trace'] = $level;
    }

    function &amp;__generate($method, &amp;$params, $namespace = false, $soapAction = false)
    {
        $this-&gt;fault = null;
        $this-&gt;__options['input']='parse';
        $this-&gt;__options['result']='parse';
        $this-&gt;__options['parameters'] = false;
        if ($params &amp;&amp; gettype($params) != 'array') {
            $params = array($params);
        }
        if (gettype($namespace) == 'array') {
            foreach ($namespace as $optname =&gt; $opt) {
                $this-&gt;__options[strtolower($optname)] = $opt;
            }
            if (isset($this-&gt;__options['namespace'])) {
                $namespace = $this-&gt;__options['namespace'];
            } else {
                $namespace = false;
            }
        } else {
            // we'll place soapaction into our array for usage in the transport
            $this-&gt;__options['soapaction'] = $soapAction;
            $this-&gt;__options['namespace'] = $namespace;
        }

        if ($this-&gt;__endpointType == 'wsdl') {
            $this-&gt;_setSchemaVersion($this-&gt;_wsdl-&gt;xsd);
            // get portName
            if (!$this-&gt;_portName) {
                $this-&gt;_portName = $this-&gt;_wsdl-&gt;getPortName($method);
            }
            if (PEAR::isError($this-&gt;_portName)) {
                return $this-&gt;_raiseSoapFault($this-&gt;_portName);
            }

            // get endpoint
            $this-&gt;_endpoint = $this-&gt;_wsdl-&gt;getEndpoint($this-&gt;_portName);
            if (PEAR::isError($this-&gt;_endpoint)) {
                return $this-&gt;_raiseSoapFault($this-&gt;_endpoint);
            }

            // get operation data
            $opData = $this-&gt;_wsdl-&gt;getOperationData($this-&gt;_portName, $method);

            if (PEAR::isError($opData)) {
                return $this-&gt;_raiseSoapFault($opData);
            }
            $namespace = $opData['namespace'];
            $this-&gt;__options['style'] = $opData['style'];
            $this-&gt;__options['use'] = $opData['input']['use'];
            $this-&gt;__options['soapaction'] = $opData['soapAction'];

            // set input params
            if ($this-&gt;__options['input'] == 'parse') {
                $this-&gt;__options['parameters'] = $opData['parameters'];
                $nparams = array();
                if (isset($opData['input']['parts']) &amp;&amp; count($opData['input']['parts']) &gt; 0) {
                    $i = 0;
                    reset($params);
                    foreach ($opData['input']['parts'] as $name =&gt; $part) {
                        $xmlns = '';
                        $attrs = array();
                        // is the name actually a complex type?
                        if (isset($part['element'])) {
                            $xmlns = $this-&gt;_wsdl-&gt;namespaces[$part['namespace']];
                            $part = $this-&gt;_wsdl-&gt;elements[$part['namespace']][$part['type']];
                            $name = $part['name'];
                        }
                        if (array_key_exists($name, $params) ||
                            $this-&gt;_wsdl-&gt;getDataHandler($name, $part['namespace'])) {
                            $nparams[$name] =&amp; $params[$name];
                        } else {
                            // we now force an associative array for
                            // parameters if using wsdl.
                            return $this-&gt;_raiseSoapFault("The named parameter $name is not in the call parameters.");
                        }
                        if (gettype($nparams[$name]) != 'object' ||
                            !is_a($nparams[$name],'soap_value')) {
                            // type is a qname likely, split it apart, and get the type namespace from wsdl
                            $qname =  new QName($part['type']);
                            if ($qname-&gt;ns) {
                                $type_namespace = $this-&gt;_wsdl-&gt;namespaces[$qname-&gt;ns];
                            } else if (isset($part['namespace'])) {
                                $type_namespace = $this-&gt;_wsdl-&gt;namespaces[$part['namespace']];
                            } else {
                                $type_namespace = null;
                            }
                            $qname-&gt;namespace = $type_namespace;
                            $type = $qname-&gt;name;
                            $pqname = $name;
                            if ($xmlns) {
                                $pqname = '{' . $xmlns . '}' . $name;
                            }
                            $nparams[$name] =  new SOAP_Value($pqname, $qname-&gt;fqn(), $nparams[$name], $attrs);
                        } else {
                            // wsdl fixups to the soap value.
                        }
                    }
                }
                $params =&amp; $nparams;
                unset($nparams);
            }
        } else {
            $this-&gt;_setSchemaVersion(SOAP_XML_SCHEMA_VERSION);
        }

        // serialize the message.
        $this-&gt;_section5 = (isset($this-&gt;__options['use']) &amp;&amp; $this-&gt;__options['use'] == 'literal');

        if (!isset($this-&gt;__options['style']) || $this-&gt;__options['style'] == 'rpc') {
            $this-&gt;__options['style'] = 'rpc';
            $this-&gt;docparams = true;
            $mqname =  new QName($method, $namespace);
            $methodValue =  new SOAP_Value($mqname-&gt;fqn(), 'Struct', $params);
            $soap_msg =&amp; $this-&gt;_makeEnvelope($methodValue, $this-&gt;headersOut, $this-&gt;_encoding, $this-&gt;__options);
        } else {
            if (!$params) {
                $mqname =  new QName($method, $namespace);
                $mynull = null;
                $params =  new SOAP_Value($mqname-&gt;fqn(), 'Struct', $mynull);
            } elseif ($this-&gt;__options['input'] == 'parse') {
                if (is_array($params)) {
                    $nparams = array();
                    $keys = array_keys($params);
                    foreach ($keys as $k) {
                        if (gettype($params[$k]) != 'object') {
                            $nparams[] =  new SOAP_Value($k, false, $params[$k]);
                        } else {
                            $nparams[] =&amp; $params[$k];
                        }
                    }
                    $params =&amp; $nparams;
                }
                if ($this-&gt;__options['parameters']) {
                    $mqname =  new QName($method, $namespace);
                    $params =  new SOAP_Value($mqname-&gt;fqn(), 'Struct', $params);
                }
            }
            $soap_msg =&amp; $this-&gt;_makeEnvelope($params, $this-&gt;headersOut, $this-&gt;_encoding, $this-&gt;__options);
        }
        unset($this-&gt;headersOut);

        if (PEAR::isError($soap_msg)) {
            return $this-&gt;_raiseSoapFault($soap_msg);
        }

        // handle Mime or DIME encoding
        // XXX DIME Encoding should move to the transport, do it here for now
        // and for ease of getting it done
        if (count($this-&gt;__attachments)) {
            if ((isset($this-&gt;__options['attachments']) &amp;&amp; $this-&gt;__options['attachments'] == 'Mime') || isset($this-&gt;__options['Mime'])) {
                $soap_msg =&amp; $this-&gt;_makeMimeMessage($soap_msg, $this-&gt;_encoding);
            } else {
                // default is dime
                $soap_msg =&amp; $this-&gt;_makeDIMEMessage($soap_msg, $this-&gt;_encoding);
                $this-&gt;__options['headers']['Content-Type'] = 'application/dime';
            }
            if (PEAR::isError($soap_msg)) {
                return $this-&gt;_raiseSoapFault($soap_msg);
            }
        }

        // instantiate client
        if (is_array($soap_msg)) {
            $soap_data =&amp; $soap_msg['body'];
            if (count($soap_msg['headers'])) {
                if (isset($this-&gt;__options['headers'])) {
                    $this-&gt;__options['headers'] = array_merge($this-&gt;__options['headers'], $soap_msg['headers']);
                } else {
                    $this-&gt;__options['headers'] = $soap_msg['headers'];
                }
            }
        } else {
            $soap_data =&amp; $soap_msg;
        }
        return $soap_data;
    }

    function &amp;__parse(&amp;$response, $encoding, &amp;$attachments)
    {
        // parse the response
        $response =  new SOAP_Parser($response, $encoding, $attachments);
        if ($response-&gt;fault) {
            return $this-&gt;_raiseSoapFault($response-&gt;fault);
        }
        // return array of parameters
        $return =&amp; $response-&gt;getResponse();
        $headers =&amp; $response-&gt;getHeaders();
        if ($headers) {
            $this-&gt;headersIn =&amp; $this-&gt;__decodeResponse($headers, false);
        }
        return $this-&gt;__decodeResponse($return);
    }

    function &amp;__decodeResponse(&amp;$response, $shift = true)
    {
        if (!$response) {
            return null;
        }
        // Check for valid response.
        if (PEAR::isError($response)) {
            return $this-&gt;_raiseSoapFault($response);
        } elseif (!is_a($response, 'soap_value')) {
            return $this-&gt;_raiseSoapFault("didn't get SOAP_Value object back from client");
        }

        // Decode to native php datatype.
        $returnArray =&amp; $this-&gt;_decode($response);

        // Fault?
        if (PEAR::isError($returnArray)) {
            return $this-&gt;_raiseSoapFault($returnArray);
        }
        if (is_object($returnArray) &amp;&amp; strcasecmp(get_class($returnArray),'stdClass') == 0) {
            $returnArray = get_object_vars($returnArray);
        }
        if (is_array($returnArray)) {
            if (isset($returnArray['faultcode']) || isset($returnArray['SOAP-ENV:faultcode'])) {
                $faultcode = $faultstring = $faultdetail = $faultactor = '';
                foreach ($returnArray as $k =&gt; $v) {
                    if (stristr($k, 'faultcode')) $faultcode = $v;
                    if (stristr($k, 'faultstring')) $faultstring = $v;
                    if (stristr($k, 'detail')) $faultdetail = $v;
                    if (stristr($k, 'faultactor')) $faultactor = $v;
                }
                return $this-&gt;_raiseSoapFault($faultstring, $faultdetail, $faultactor, $faultcode);
            }
            // Return array of return values.
            if ($shift &amp;&amp; count($returnArray) == 1) {
                return array_shift($returnArray);
            }
            return $returnArray;
        }
        return $returnArray;
    }

    function __get_wire()
    {
        if ($this-&gt;__options['trace'] &gt; 0 &amp;&amp; ($this-&gt;__last_request || $this-&gt;__last_response)) {
            return "OUTGOING:\n\n".
            $this-&gt;__last_request.
            "\n\nINCOMING\n\n".
            preg_replace("/&gt;&lt;/","&gt;\r\n&lt;", $this-&gt;__last_response);
        }
        return null;
    }

}
</code></pre>

<p><strong>EDIT</strong>:</p>

<p>I have typed <code>...paypal$ grep -R 'Client.php' *</code>, and I'm getting this:</p>

<pre><code>PayPal/CallerServices.php://require_once 'PayPal/SOAP/Client.php';
PayPal/SOAP/Client.php:// $Id: Client.php,v 1.1.1.1 2006/02/19
PayPal/SOAP/WSDL.php: //require_once 'PayPal/SOAP/Client.php
</code></pre>

<p>It's for sure that there are no more files in my app that could include Client.php.</p>

<p>Javi</p>

<p>Regards</p>

<p>Javi</p>
