免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4790 | 回复: 11
打印 上一主题 下一主题

[讨论]PHP4.3.8中类的方法中使用global [复制链接]

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-10-12 14:35 |只看该作者 |倒序浏览
讨论点
在类的方法中使用global传递参数是否可行?

例如:
$cfg='test';
class test_class {
...
    function getVar(){
        global $cfg;
        ...
    }
}

论坛徽章:
0
2 [报告]
发表于 2004-10-12 17:19 |只看该作者

[讨论]PHP4.3.8中类的方法中使用global

是这样的阿~

我喜欢设全局数组 $CONF  = array();
然后在方法中调global $CONF,或者直接用$GLOBALS['CONF']

论坛徽章:
0
3 [报告]
发表于 2004-10-13 09:41 |只看该作者

[讨论]PHP4.3.8中类的方法中使用global

恩,仔细看了标题是4.3.8

我现在用4.3.6,是不是关于global变量有改动阿?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2004-10-13 09:42 |只看该作者

[讨论]PHP4.3.8中类的方法中使用global

OK,有兴趣的来做个实验吧.

test.php
  1. <?php
  2. function showVar() {
  3. include 'class_test.php';
  4.         $obc        = new testClass();
  5.         $obc->;showVar();
  6. }
  7. showVar();
  8. ?>;
复制代码


class_test.php
  1. <?php
  2. $tmpVar        = "test data";
  3. class testClass {
  4.         var $link;
  5.         function showVar() {
  6.                 global $tmpVar;
  7.                 var_dump($tmpVar);
  8.         }
  9. }
  10. ?>;
复制代码


先运行上面的test.php
然后把test.php改成下面的代码试试看.
  1. <?php
  2. include 'class_test.php';
  3. function showVar() {
  4.         $obc        = new testClass();
  5.         $obc->;showVar();
  6. }
  7. showVar();
  8. ?>;
复制代码
longnetpro 该用户已被删除
5 [报告]
发表于 2004-10-13 10:11 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
6 [报告]
发表于 2004-10-13 10:28 |只看该作者

[讨论]PHP4.3.8中类的方法中使用global

楼上这么感兴趣实验,那再帮我试一下, 看看

$var  = 'xxx';
function a()
{
    global $var;
    echo $var;
}
的$var


跟 $GLOBALS['var'] = 'xxx'; 的 $var 有差别没? 谢谢了, 呵呵
longnetpro 该用户已被删除
7 [报告]
发表于 2004-10-13 10:40 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
8 [报告]
发表于 2004-10-13 10:47 |只看该作者

[讨论]PHP4.3.8中类的方法中使用global



还是老大强啊!

当时我就晕了.后来给PHP发邮件了.看看他们会不会回复.

再问一句,在c/c++或dephi中关于全局变量的定义是不是也是这样?
longnetpro 该用户已被删除
9 [报告]
发表于 2004-10-13 14:07 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
10 [报告]
发表于 2004-10-13 15:30 |只看该作者

[讨论]PHP4.3.8中类的方法中使用global

他们的回复全文如下(汗~~~):

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

No bug here, you include the class inside a function, so the variable is
not put in the global scope but in the the function that called the
include.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP