为什么使用“全局"被认为是不好的做法?内部函数引用? [英] Why is it considered bad practice to use "global" reference inside functions?

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

问题描述

可能的重复:
PHP 中的全局变量是否被认为是不好的做法?如果是,为什么?
全局函数

上面链接中回答的问题.

question answered in link above.

不,php 中的global"与其他语言中的 global 不是一回事,虽然它不会引入任何安全问题,但会使其他人难以理解代码.

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.

操作:

项目摘要 - 我正在编写一个网络 CMS 来让我对 PHP/MySQL 有所了解.为了分解代码,我有这些基本层/模块的概念:

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:

数据
- MySQL 表
- PHP 变量

Data
- MySQL Tables
- PHP Variables

功能
- SQL - 获取/设置/等
- 前端 - 显示页面
- 后端 - 经理

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

介绍
- HTML 模板
- 页面内容
- CSS 样式表

Presentation
- HTML Templates
- Page Content
- CSS Stylesheets

目标很常见.使用 MySQL 保存站点设置和页面内容,使用 php 获取/操作正在提供的页面的内容数据,然后插入到 html 模板并回显到浏览器.来自像 C# 这样的面向对象语言,我遇到的第一个问题是使用包含和函数时的变量范围问题.

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.

从一开始我就一直在编写纯函数的 php 文件,并将它们包含在其他具有现有变量数组定义的文件中需要的地方.例如,暂时忽略数据层,一个简单的页面可能通常如下所示:

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:

文件 1(页)

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

include 'functions.php';

get_data ( 'page.php' );

[...]

run_html ();

文件 2(函数)

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:

  • sql fetch 后这些数组中的数据可能在任何地方使用,包括页面内容
  • 我不想有十几个函数参数,也不想传递整个数组

代码运行良好.但是在我找到的关于该主题的每篇文章中,我的函数中的全局"调用都被称为不好的做法,尽管这些文章从未说明原因?我认为这意味着使用父范围".是否在我的应用程序中引入了安全漏洞?有没有更好的方法?提前致谢.

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.

您的函数 get_datarun_html 不会以任何方式宣传它们共享数据,但它们确实以一种很大的方式进行了宣传.并且没有办法(没有阅读代码)知道如果 get_data 没有被调用,run_html 将是无用的.

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天全站免登陆