Variable references in PHP & cannot be used in conjunction with global

Source: Internet
Author: User

Source of the problem, new company Debug.

The code in the program is roughly the following

Class Ci {private static $instance = NULL;        Public $name = ' Hello ';        public $load = null;        Public Function __construct () {self:: $instance = & $this;        $this->load = new Load;        Mlog (self:: $instance, __line__);//other code .... $this->name = ' My name ';    }public static function &getinstance () {return self:: $instance;}        } class My extends ci{public function __construct () {parent::__construct (); $this->model = new model; }} class Model {public Function __construct () {$this->name = _get ();//Nullmlog ("Success, and model->  Name = {$this->name} ", __line__);}  } class Load {public $name = ' I am Load class ';p ublic function GetName () {return $this->name;}    } $CI =& get_instance (); Mlog ($CI->name); Hello//Mlog ($CI->load->getname ());  I am Load class Mlog ($CI, __line__);  $CI = new My;  Mlog ($CI, __line__); function _get () {//call static Variablesglobal $CI; Non-boject//return $CI->load->name (); 500return $CI->name;  NOTICE} function &get_instance () {return ci::getinstance (); } function Mlog ($content, $line = __line__, $display = True) {$next _line = "<br/>"; if (php_sapi_name () = = = ' CLI ')  {$init = '; $next _line = "\ n";}  if ($display) {echo $init; echo "In line: $line".  $next _line;  Print_r ($content);  echo $next _line;} }

I believe friends can see that this is part of the CI framework, and developers use the following code in helper for the encapsulation method

<?php    $CI =& get_instance ();    function Demo ()     {         global $CI;         return $CI->mehton;          Notice         //return $CI->class->method;//500       }

The server directly reported 500. Through my first part of the code and comments, it should be possible to draw the following conclusions: In the function scope of the global $ref is not to get the reference, the PHP manual is also described in the details, see. You can't do anything but be careful, learn the basics ^_^

Variable references in PHP & cannot be used in conjunction with global

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.