函数传递对象方式

有利于理解PHP变量在内存的形式,变量在栈区有自己空间、内容、地址,下面代码方便理解。
<?php
 
$a = 100;
$b = $a;
$b = $b+20;
echo $a;
echo "<br>";
echo $b;
//结果是 $a = 100 $b = 120;
 
 
 
 
$a = 100;
$b = &$a;
$b = $b+20;
echo $a;
echo "<br>";
echo $b;
  //结果是$a=120  $b = 120
 
 
   class Waf
    {
        public $name;
        public $version;
    }
 
    $web          = new Waf();
    $web->name    = "Web Application Firewall";
    $web->version = 123.456;
    $b            = $web;
    echo $web->name;
    echo "<br>";
    echo $b->name;
    //结果都是:Web Application Firewall 
 
 
    class Waf
    {
        public $name;
        public $version;
    }
 
    $web          = new Waf();
    $web->name    = "Web Application Firewall";
    $web->version = 123.456;
 
    function test1($p)
    {
        $p->name="IPS";
    }
    test1($web);
    echo $web->name;
 
   // 结是IPS,这里function 接收的变量$p 其实就是$web对象的原地址而不变量的值.

标签: php, 函数传递对象方式