分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

php中变量引用&不可与global共同使用

发布时间:2023-09-06 01:13责任编辑:林大明关键词:暂无标签

  问题来源,新公司debug。

  程序中代码大致如下

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‘;public 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;}  }

  相信朋友们可以看出来,这是CI框架中的一部分,开发者为了封装方法所以在helper中使用如下代码

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

  服务器直接报了500。通过我第一部分的代码与注释,应该可以得出以下结论:在function的scope中global $ref是拿不到该引用的,php手册中也有说明,详情请看。干啥都不能不细心啊,好好学习基础^_^

php中变量引用&不可与global共同使用

原文地址:http://www.cnblogs.com/jixingke/p/7573042.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved