LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1460|回复: 7

关于php中的&在此处的含义

[复制链接]
发表于 2008-3-7 11:45:01 | 显示全部楼层 |阅读模式
[php]
class a
{
function &parse($request)
{
......
}
}
[/php]

这里的&是什么含义?

如果需要我可以贴全代码
发表于 2008-3-7 12:11:40 | 显示全部楼层
返回值引用,这种函数必须返回一个有效的变量。

如 return $var;
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-3-7 12:48:27 | 显示全部楼层
这样做有什么作用?
回复 支持 反对

使用道具 举报

发表于 2008-3-7 13:04:47 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-3-7 13:28:01 | 显示全部楼层
[php]
<?php
class A
{
  var $v;
  function &inc($v)
  {
    $amount =1;
    $v += $amount;
    return $v;
  }
  function get_inc()
  {
    return $this->v;
  }
/*
  function __get()
  {
    $
  }
*/

}
/*
$value = 10;
&inc ($value);
echo $value;
*/
$v1 = 10;

$a = &new A();

echo $a->inc($v1) . "\n";
$v2 =& $a->get_inc();
echo $v2 . "\n";
var_dump($v2);
echo "\n";

?>

[/php]
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-3-7 16:37:12 | 显示全部楼层
PEAR/HTTP/Server/Request.php
[php]
<?PHP

/* vim: set expandtab tabstop=4 shiftwidth=4: */

// +----------------------------------------------------------------------+

// | PHP Version 4                                                        |

// +----------------------------------------------------------------------+

// | Copyright (c) 1997-2002 The PHP Group                                |

// +----------------------------------------------------------------------+

// | This source file is subject to version 2.0 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: Stephan Schmidt <schst@php-tools.net>                       |

// +----------------------------------------------------------------------+

//

//    $Id: Request.php,v 1.4 2005/01/01 11:28:29 schst Exp $



/**

* HTTP_Server_Request

*

* Interface that parses the request

*

* @author  Stephan Schmidt <schst@php-tools.de>

*/

class HTTP_Server_Request {



   /**

    * method

    *

    * @access   public

    * @var      string

    */

    var $method;



   /**

    * protocol

    *

    * @access   public

    * @var      string

    */

    var $protocol;



   /**

    * uri

    *

    * @access   public

    * @var      string

    */

    var $uri;



   /**

    * path info

    *

    * @access   public

    * @var      string

    */

    var $path_info;



   /**

    * query string

    *

    * @access   public

    * @var      string

    */

    var $query_string;



   /**

    * headers

    *

    * @access   public

    * @var      array

    */

    var $headers    =   array();

   

    /**

    *   the data (like POST) sent after the headers

    *   @access public

    *   @var    string

    */

    var $content    = '';



   /**

    * parse a http request

    *

    * @access    public

    * @static

    * @param     string    $request    raw request data

    * @return    array     $request    parsed request

    */

    function &parse($request)

    {

        //    split lines

        $lines = explode ("\r\n", $request);



        //    check for method, uri and protocol in line 1

        $regs = array();

        if (!preg_match("'([^ ]+) ([^ ]+) (HTTP/[^ ]+)'", $lines[0], $regs)) {

            return false;

        }



        $request = &new HTTP_Server_Request();

        

        $request->method   = $regs[1];

        $request->uri      = $regs[2];

        $request->protocol = $regs[3];

            

        //    parse the uri   

        if ($tmp = HTTP_Server_Request::_parsePath($regs[2])) {

            $request->path_info    = $tmp['path_info'];

            $request->query_string = $tmp['query_string'];

        }

   

        //    parse and store additional headers (not needed, but nice to have)

        /**

        * FIXME:

        *   HTTP1.1 allows headers be broken on several lines if the next line begins with

        *   a space or a tab (rfc2616#2.2)

        */

        for ($i = 1; $i < count($lines); $i++) {

            if (trim($lines[$i]) == '') {

                //empty line, after this the content should follow

                $i++;

                break;

            }

            $regs    =    array();

            if (preg_match("'([^: ]+): (.+)'", $lines[$i], $regs)) {

                $request->headers[(strtolower($regs[1]))]    =    $regs[2];

            }

        }

        //aggregate the content (POST data or so)

        $request->content = '';

        for ($i = $i; $i < count($lines); $i++) {

            $request->content .= $lines[$i] . "\r\n";

        }

        

        return $request;

    }



   /**

    * get the request method

    *

    * @access   public

    * @return   string

    */

    function getMethod()

    {

        return $this->method;

    }

   

   /**

    * get path info

    *

    * @access   public

    * @return   string

    */

    function getPathInfo()

    {

        return $this->path_info;

    }

   

   /**

    * parse a request uri

    *

    * @access    public

    * @param    string    $path    uri to parse

    * @return    array    $path    path data

    */

    function _parsePath($path)

    {

        $regs = array();

        if (!preg_match("'([^?]*)(?:\?([^#]*))?(?:#.*)? *'", $path, $regs)) {

            return false;

        }



        return array(

                      'path_info'    => $regs[1],

                      'query_string' => isset($regs[2]) ? $regs[2] : null

                  );

    }

   

   /**

    *   Exports server variables based on request data

    *   like _GET, _SERVER[HTTP_*] and so

    *   The function can be used to make your own

    *   HTTP server act more than a "real" one (like apache)

    *

    *   @access public

    */

    function export()

    {

        //_SERVER[HTTP_*] from headers

        foreach ($this->headers as $strId => $strValue) {

            $_SERVER['HTTP_' . str_replace('-', '_', strtoupper($strId))] = $strValue;

        }

        

        $nPos = strpos($this->headers['host'], ':');

        if ($nPos !== false) {

            $_SERVER['HTTP_HOST']   = substr( $this->headers['host'], 0, $nPos);

            $_SERVER['SERVER_PORT'] = substr( $this->headers['host'], $nPos);

        } else {

            $_SERVER['SERVER_PORT'] = 80;

        }

        

        $_SERVER['QUERY_STRING']    = $this->query_string;

        $_SERVER['SERVER_PROTOCOL'] = $this->protocol;

        $_SERVER['REQUEST_METHOD']  = $this->method;

        $_SERVER['REQUEST_URI']     = $this->uri;

        

        //GET variables

        parse_str($this->query_string, $_GET);

        if (count($_GET) == 0) {

            $_SERVER['argc'] = 0;

            $_SERVER['argv'] = array();

        } else {

            $_SERVER['argc'] = 1;

            $_SERVER['argv'] = array($this->query_string);

        }

        

        //@todo: POST, COOKIE, FILES, SESSION,....

    }

}

?>
[/php]
回复 支持 反对

使用道具 举报

发表于 2008-3-9 09:53:05 | 显示全部楼层
好贴,值得探讨.
回复 支持 反对

使用道具 举报

发表于 2008-3-9 09:55:35 | 显示全部楼层
我认为  &   就该是一个取内存地址的运算符,就象C里面的指针引用一样.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表