引用返回和取消引用 简单

////////////////////////////////////////////////////////////
//@date:2013-02-20
//引用返回
//引用返回用在当想用函数找到引用应该被绑定在哪一个变量上面时
//不要用返回引用来增加性以,引擎足够聪明明来自己进行优化,
//仅在有合理的技术原因时才返回引用!要返回引用

// class foo
// {
//     public $value = 42;

//     public function &getValue()
//     {
//          return $this->value;
//     }
// }
// $obj = new foo();
// $myValue = &$obj->getValue();
// //$myValue = $obj->getValue();
// $obj->value = 3;
// var_dump($myValue); //3
//本例中getValue函数所返回的对象的属性将被赋值,而不是copy,就和没有用引用语法的一样
//和参数传递不同,这里必须在两个地方都用&符号,指出返回的是一个引用,而不是通常的一个copy,同样也指出$myValue是作为引用的绑定,而不是通常的赋值


// $a = 0;
// function &b()
// {
//      global $a;
//      return $a;
// }

// $c = &b();
// $c++;
// echo "a:{$a},c:{$c}<BR>";

// function &func()
// {
//     static $static = 0;
//     $static++;
//     return $static;
// }
// $var1 = &func();
// echo "var1:{$var1}<BR>"; //1
// func();
// func();
// echo "var1:{$var1}<BR>"; //3
// $var2 = func(); //这里没有用引用了,只是一个普通的用法
// echo "var2:{$var2}<BR>"; //4 这里为什么是4。因为以前已经是3了然后加一返回
// func(); //这里不会在对$var2进行自加了,因为返回的只是一个变量,并不是一个地址
// func();
// echo "var1:{$var1}<BR>"; //6
// echo "var2:{$var2}<BR>"; //4


// class Foo
// {
//     protected $bar;

//     public function __construct()
//     {
//          $this->bar = new Bar();         
//     }

//     public function getBar()
//     {
//          return $this->bar;
//     }
// }

// class Bar
// {
//      public function __construct()
//      {

//      }

//      public function helloWorld()
//      {
//          echo "Hello World<BR><BR>";
//      }
// }

// function test()
// {
//      return new Foo();
// }
//test()->getBar()->helloWorld();

// class foo
// {
//     private $value = 42;
//     public function &getValue()
//     {
//           return $this->value;
//     }

//     public function echoValue()
//     {
//           echo "this value:".$this->value."<BR><BR>";
//     }
// }

// $obj = new foo();
// $myValue = &$obj->getValue();
// $myValue = 22;
// $obj->echoValue();


// class some_class
// {
//     function &func_b()
//     {
//          $this->some_var = 2;
//          return $this->some_var;
//     }

//     function func_a(&$param)
//     {
//           $param = $this->func_b();
//     }
// }
// $var = 1;
// $obj = new some_class();
// $obj->func_a($var);
// echo $var."<BR><BR>"; //2

// $var = 5;
// echo $obj->some_var; //2


// class TestClass
// {
//      var $thisVar = 0;

//      function TestClass($var)
//      {
//           $this->thisVar = $var;
//      }

//      static function &getTestClass($value)
//      {
//             static $classes;

//             if(!isset($classes[$value]))
//             {
//                  $classes[$value] = new TestClass($value);
//             }
//             return $classes[$value];
//      }
// }


// $class1 = & TestClass::getTestClass(432);
// echo "value is ".$class1->thisVar."<BR><BR>";


// $class2 = & TestClass::getTestClass(342);
// echo "value is ".$class2->thisVar."<BR><BR>";

// $class1->thisVar = 3333;
// echo "value is ".$class1->thisVar."<BR><BR>";



// function& getref1()
// {
//   $ref =& $GLOBALS['somevar'];
//   return ($ref);
// }
// $a = &getref1();
// echo $a;



// function& getref2()
// {
//   $ref = 42;
//   return ($ref);
// }
// $b = &getref2();
// echo $b;
// $b++;
// echo $b;



// // Will return a reference
// function& getref3()
// {
//   static $ref = 42;
//   return ($ref);
// }
// $c = &getref3();
// echo "<BR><BR>";
// echo $c;
// $c++;
// echo $c;


// class sample_singleton
// {
//      protected static $instance = null;

//      private function __construct()
//      {

//      }

//      public static function create()
//      {
//            self::$instance = new sample_singleton();

//      }

//      public static function getInstance()
//      {
//            return self::$instance;
//      }

//      public function say()
//      {
//           echo "wuow";
//      }
// }

// sample_singleton::create();
// $obj = sample_singleton::getInstance();
// $obj->say();


// $a=1;
// function &foo()
// {
//   global $a;
//   return isset($a)?$a:null;
//   //return $a; 主要还是在这里,如果直接返回,就是引用了,
//   //但这里是经过copy过后的,所以....
//   //为什么经过copy了?
// }
// $b=&foo();
// echo $b;   // shows 1
// $b=2;
// echo $a;   // shows 1 (not 2! because $b got a copy of $a)


//请记住,通过引用返回不起作用__ callStatic:
// class Test
// {
     
//      private static $_inst;

//      public static function & __callStatic($name, $args)
//      {
//            // if(!isset(static::$_inst))
//            // {
//            //      echo "crteate";
//            //      static::$_inst = (object)"test";
//            // }
//    //        if (!isset(static::$_inst)){ 
//    //    echo "create"; 
//    //    static::$_inst = (object)"test"; 
//    // } 
//            return static::$_inst;
//      }
// }
// var_dump($a = &Test::abc()); //create
// $a = null;
// var_dump(Test::abc()); //null
// class Test { 
//   private static $inst=null; 
//   public static function & __callStatic ($name, $args) { 
//    //  if (!isset(static::$inst)){ 
//    //    echo "create"; 
//    //    static::$inst = (object)"test"; 
//    // } 
//    return static::$inst; 
// } 
// $a = &Test::abc()
//var_dump(); // prints 'create' 
// $a = null; 
// var_dump(Test::abc()); // doesn't prints and the instance still exists in Test::$_inst 
//这个例子走不通,官方的也一样



////////////////////////////////////////////////////////////////
//@date: 2013-02-20
//取消引用
//当unset一个引用,只是断开了变量名和变量内容之间的绑定,这并不意味着变量内容被销毁了
// $a = 1;
// $b = &$a;
// $b = NULL;
// //echo "a:{$a},b:{$b}<BR><BR>";


// $a = 1;
// $b = &$a;
// $c = &$b;
// $b = NULL;
// echo "a:{$a},b:{$b},c:{$c}<BR><BR>";

// function test(&$trash)
// {
//      unset($trash);
// }


// function nuke_me($var)
// {
//       if(is_object($var) || in_array($var))
//       {
//          foreach($var as $key=>$value)
//          {
//               if(isset($key))
//               {
//                     if(is_object($var)){
//                          unset($var->$key);
//                     }
//                     if(is_array($var))
//                     {
//                           unset($var[$key]);
//                     }
//               }
//          }    
//       }else
//          unset($var);
// }



// $a->something = true;
// var_dump($a);
// echo "<BR><BR>";

// test($a); //unset不管用
// var_dump($a);
// echo "<BR><BR>";


// nuke_me($a); //这个才起unset作用
// var_dump($a);
// echo "<BR><BR>";


// $a = "hihaha";
// $b = &$a;
// $c = "eita";
// $b = $c;
// echo "1:".$a."<BR><BR>";

// $a = "hihaha";
// $b = &$a;
// $c = "eita";
// $b = &$c;
// echo "2:".$a."<BR><BR>"; //hihaha

// $a = "hihaha";
// $b = &$a;
// $b = null;
// echo "3:".$a."<BR><BR>"; //null

// $a = "hihaha";
// $b = &$a;
// unset($b);
// echo "4:".$a."<BR><BR>"; //hihaha


// $a = "hihaha";
// $b = &$a;
// $c = "eita";
// $a = $c;
// echo "5 b:".$b."<BR><BR>"; //eita


// $a = "hihaha";
// $b = &$a;
// $c = "eita";
// $a = &$c; //这里不行哦,并不是函数返回引用
// echo "6 b:".$b."<BR><BR>"; //hihaha


// $a = "hihaha";
// $b = &$a;
// $a = null;
// echo "7 b:".$b."<BR><BR>"; //null


// $a = "hihaha";
// $b = &$a;
// unset($a); //只是去掉$a的变量名,并没去掉$a所指向内容的值
// echo "8 b:".$b."<BR><BR>"; //hihaha



//注意,在前面的例子中,所有变量(或一个数据项的所有变量指向)设置为NULL,!isset()函数被解释为,但变量之间的联系依然存在,所以



// echo (isset($a)?"set":"unset")."<BR><BR>";
// //unset? 没有变量$a,咱还是unset呢

// $a = 1;
// $b = &$a;
// echo (isset($b) ? "set" : "unset")."<BR><BR>"; //set

// $a = null;
// echo (isset($b) ? "set" : "unset")."<BR><BR>"; //unset
// //这里只是为$a赋值为null


// $a = 1;
// echo (isset($b) ? "set" : "unset")."<BR><BR>"; //set
// //理解不进去了,重新给变量$a赋值, 但$b需要重新创建


// $foo = "Bob";
// $bar = &$foo;
// $bar = "my name is ".$bar;
// echo $bar."<BR>"; //my name is Bob
// echo $foo."<BR>"; //my name is Bob

// $foo = "I am Frank";
// echo $bar."<BR>"; //I am Frank
// echo $foo."<BR>"; //I am Frank

// $foobar = &$bar;
// $foobar = "hello ".$foobar; 
// echo $foobar."<BR>"; //hello I am Frank

// // unset($bar);
// // echo $foo."<BR>"; //hello I am Frank
// // echo $bar."<BR>"; //null


// $bar = null;
// echo $foo."11<BR>"; //
// echo $bar."22<BR>"; //



// $a = 1;
// $b = & $a;
// $c = & $b;
// //$b = NULL;
// $c = NULL;
// echo "a:{$a},b:{$b},c:{$c}<BR><BR>";

  

原文地址:https://www.cnblogs.com/xiangxiaodong/p/2919839.html