What is =& in PHP?

前端 未结 5 1457
甜味超标
甜味超标 2021-01-17 11:17

Look at this example. There is a line:

  $client =& new xmlrpc_client(\'/xml-rpc\', \'api.quicktate.com\', 80);
  $client->return_type = \'xmlrpcvals         


        
相关标签:
5条回答
  • 2021-01-17 11:44

    The = and the & should* have a space between them - they're two different operators. The & means get a reference to this.

    The -> is for object member access - this means assign 'xmlrpcvals' to the return_type member of $client.

    * see comments for clarification

    0 讨论(0)
  • 2021-01-17 11:48

    Is to pass a variable by reference

    <?php  
       $a = 5; 
       $b =& $a; 
       $b = 6; 
    
       echo "a: "; 
       var_dump($a); 
       echo "b: "; 
       var_dump($b); 
    ?>
    

    output:

    a: int(6)
    b: int(6) 
    
    0 讨论(0)
  • 2021-01-17 11:48

    This is called returning by reference.

    0 讨论(0)
  • 2021-01-17 11:59

    Starting with the last question first;

    what is the -> in $client->return_type mean?

    -> is the operator you use to access properties and methods of an object in PHP. Most languages, such as Java or Javascript use the dot operator for the same thing. It (probably) derives from the C syntax for accessing members of a struct.

    Then that first question...

    what is the =& ?

    The short version is, in your example, it's a relic you no longer need; a hangover from PHP4 which you no longer need if you use PHP5. But note this is specific to your example.

    For the long version, so you really understand what's going on read http://derickrethans.nl/talks/phparch-php-variables-article.pdf about References in PHP

    0 讨论(0)
  • 2021-01-17 12:04

    The =& assigns the variable a reference to the object rather than copying it. It is two separate operators (assignment and getting a reference) but they are often written together.

    The -> is a member access operator; in the example it means to get the return_type that belongs to the XML-RPC client.

    0 讨论(0)
提交回复
热议问题