为什么认为不好的做法是使用“全局”参考里面的函数? [英] Why is it considered bad practice to use "global" reference inside functions?

查看:125
本文介绍了为什么认为不好的做法是使用“全局”参考里面的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

PHP中的全局变量被认为是不好的做法?如果是这样,为什么?

全局功能


编辑:以上链接回答的问题。

不,php中的global与其他语言中的global不同,虽然它没有引入任何安全问题,但它可以使代码不易被其他人理解。



项目总结 - 我正在编写一个web CMS来让我的脚受到PHP / MySQL的影响。为了分解代码,我有了这些基本层/模块的概念:

数据

- MySQL Tables

- PHP变量



函数

- SQL - 获取/设置/ etc

- 前端 - 显示页面

- 后台 - 管理器


演示文稿
- HTML模板

- 页面内容

- CSS样式表



这个目标非常普遍。使用MySQL来保存站点设置和页面内容,使用PHP获取/操作正在服务的页面的内容数据,然后插入到html模板中并回显给浏览器。来自OO语言,比如C#,遇到的第一个问题是使用包含函数和函数时发生的可变范围问题。



从一开始,我一直在编写函数式php文件和包括他们在需要的其他文件中存在可变数组定义。例如,暂时忽略数据层,一个简单的页面可能一般如下所示:



文件1(页面)

  $ DATA_PAGE =数组

'temp'= null,
'title'= null,
[.. 。]
);

包含'functions.php';

get_data('page.php');

[...]

run_html();

文件2(功能)



<$ p $函数get_data($ page_name)
{
global $ DATA_PAGE;

$ DATA_PAGE ['temp'] ='模板';
$ DATA_PAGE ['title'] ='测试页';
[...]
}

函数run_html()
{
global $ DATA_PAGE;

echo'< html>';
echo'< head>';
echo'< title>'。 $ DATA_PAGE ['title']。 < /标题>;
[...]
}

原因:
$ b


  • sql提取后的这些数组中的数据可能在任何地方使用,包括页面内容

  • 我不想有十几个函数参数,或者传递整个数组。



代码运行良好。但是在每一篇关于这个主题的文章中,我的功能中的全局调用被称为坏习惯,即使这些文章从未说明原因?我认为这一切意味着使用父范围。我是否在我的应用中引入安全漏洞?有更好的方法吗?提前致谢。

解决方案

我认为避免这种情况的首要原因是隐藏了依赖关系。


$ b

您的函数 get_data run_html 他们共享数据的任何方式,但他们都以很大的方式。如果 get_data 还没有出现,那么就没有办法了解 run_html 是没有用的



随着代码库复杂性的增长,这种潜伏的依赖会让你的代码变得脆弱而难以理解。


Possible Duplicate:
Are global variables in PHP considered bad practice? If so, why?
global in functions

Edit: question answered in link above.

No, "global" in php is not the same thing as global in other languages, and while it does not introduce any security problems it can make the code less comprehensible to others.


OP:

Project summary - I am writing a web CMS to get my feet wet with PHP / MySQL. In order to break up the code I have a concept of these basic tiers / modules:

Data
- MySQL Tables
- PHP Variables

Function
- SQL - Get / Set / etc
- Frontend - Showing pages
- Backend - Manager

Presentation
- HTML Templates
- Page Content
- CSS Stylesheets

The goal is common enough. Use MySQL to hold site settings and page content, use php to get / manipulate content data for a page being served, then insert into an html template and echo to browser. Coming from OO languages like C# the first problem I ran into was variable scope issues when using includes and functions.

From the beginning I had been writing function-only php files and including them where needed in other files that had existing variable array definitions. For example, ignoring the data tier for a moment, a simple page might generically look like this:

File 1 (page)

$DATA_PAGE = Array
(
  'temp'  = null,
  'title' = null,
  [...]
);

include 'functions.php';

get_data ( 'page.php' );

[...]

run_html ();

File 2 (functions)

function get_data ( $page_name )
{
  global $DATA_PAGE;

  $DATA_PAGE [ 'temp'  ] = 'template';
  $DATA_PAGE [ 'title' ] = 'test page';
  [...]
}

function run_html ()
{
  global $DATA_PAGE;

  echo '<html>';
  echo '<head>';
  echo '<title>' . $DATA_PAGE [ 'title' ] . '</title>';
  [...]
}

I chose this method for a few reasons:

  • The data in these arrays after sql fetch might be used anywhere, including page content
  • I didnt want to have a dozen function arguments, or pass entire arrays

The code runs great. But in every article I find on the subject, the "global" calls in my functions are called bad practice, even though the articles never state why? I thought all that meant was "use parent scope". Am in introducing a security hole into my app? Is there a better method? Thanks in advance.

解决方案

I think a top reason for avoiding this is that it hides dependencies.

Your functions get_data and run_html do not advertise in any way that they share data, and yet they do, in a big way. And there is no way (short of reading the code) to know that run_html will be useless if get_data has not been called.

As the complexity of your codebase grows, this kind of lurking dependency will make your code fragile and hard to reason about.

这篇关于为什么认为不好的做法是使用“全局”参考里面的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆