duanbiao4025 2013-01-25 02:48
浏览 56
已采纳

php全局配置和应用程序中的设置

I have a file 'config.php' which Is included at the beginning of my program (see below) Main file runs require 'config.php';

config.php

<?
global $config;
$config['tblist'] = 'pending'; 
$config['tbdone'] = 'checked';
$config['checkfreq'] = 24;
?>

I then create a new page object

$page = new Page($name,$source);

One of the functions in that page object I want to be able to grab stuff from the config but it doesn't seem to be able to reference to $config. Have I put global $config; in the right place?

  • 写回答

5条回答 默认 最新

  • dongyao8698 2013-01-25 02:50
    关注

    When making references to global variables in functions, you must declare them as global:

    global $config;

    This is also interesting: http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/

    There is no way to declare a variable. Variables that don’t exist are created with a null value when first used.

    Global variables need a global declaration before they can be used. This is a natural consequence of the above, so it would be perfectly reasonable, except that globals can’t even be read without an explicit declaration—PHP will quietly create a local with the same name, instead. I’m not aware of another language with similar scoping issues.

    To be fair it will also throw a Notice if you've got them enabled.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址