是否使用像这样的全局变量来减少对数据库可接受实践的调用? [英] Is using global variables like this to reduce calls to a database acceptable practice?

查看:86
本文介绍了是否使用像这样的全局变量来减少对数据库可接受实践的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我使用这样的变量,那该怎么办:

  function get_stuff(){
global $ stuff;
if(!stuff)$ stuff ='candy';
返回$ stuff;
}

函数screen_output(){
$ stuff = get_stuff;
echo $ stuff;
}

这是否使代码比两个函数中的'global'更好?



ps:如果你想知道为什么我需要使用全局变量,那是因为我需要从数据库中检索一些设置,而且我不想从每个函数调用数据库。理想情况下,您将有一个处理数据的类并在数据实例化时被检索和/或用于来自DB的每个记录或一组记录。以这种方式创建的对象将根据需要从函数传递到函数。
这种想法通常会引导人们最终使用对象关系映射软件,该软件专门用于处理将数据移入或移出数据库或其他持久性(ish)存储。也就是说,理解ORM的目的需要一些关于面向对象编程的知识和范式的常用模式,因此研究OOP可能是一个更好的开始寻找答案的地方。一个维基百科对这些主题的搜索将是一个好主意。



作为一般规则,考虑到计算机系统更好(更可测试,可修复,可扩展,可替换)各个部分彼此封装(隔离)。因此,通过以全局方式做事(这不仅仅是宣布全局性的东西),你将系统中可能不需要连接的部分相互连接起来。

这种隔离是如何完成的,以及由您和您正在构建的系统类型决定的。如果您只是编写一些简单的测试代码或简单的原型,您可以尽可能多地滥用全局空间,因为代码永远不会看到白天的亮点(此处的关键字很简单)。如果您正在构建一个简单的脚本来帮助生成一对网页,并且该脚本永远不会演变成更多,那么您仍然不必担心全球空间太多。但是一旦你超越微不足道,你应该开始孤立系统的各个部分,因为它会为你提供更好的代码来处理这些问题(这意味着一旦输入和编译就会很快)



项目越复杂,零件应该越孤立,这样一个区域的变化就不会在整个系统中波及。这种涟漪可能被看作是不得不在一百个地方改变代码中的某些东西,而不是仅仅改变一些东西,或者在事后发生的错误中产生错误,因为并非所有东西都被改变或改变了正确的东西(人为错误总是会赢得胜利这里)。


So I got a lot of responses from this site that global variables are bad and kill puppies.

What if I use such variables like this:

function get_stuff(){
  global $stuff;
  if(!stuff) $stuff = 'candy';
  return $stuff;
}

function screen_output(){
  $stuff = get_stuff;
  echo $stuff;
}

does this make the code better than using 'global' in both functions?

ps: If you want to know the reason why I need to use a global variable, it's because I need to retrieve some settings from the database, and I don't want to call the db from every function.

解决方案

Ideally, you would have a class that handles the data and is instantiated when the data is retrieved and/or used for each record or set of records from the DB. The object created in this way, would then be passed from function to function as needed. This thinking normally leads people to eventually using Object Relational Mapping software, which is specifically designed to handle moving data in and out of a database or other persistent(ish) storage. That said, understanding the purpose of ORMs requires some knowledge of Object Oriented Programming and the commonly used patterns of the paradigm, so researching OOP might be a better place to start looking for answers. A wikipedia search on these topics would be a good idea.

As a general rule, consider that computer systems are better(more testable, fixable, extensible, replaceable) when the individual parts are encapsulated(isolated) from one another. So by doing something in a global way(this goes well beyond just declaring something global) you are interconnecting parts of the system that probably don't need to be connected.

How this isolation is done and to what degree is determined by you and the type of system you are building. If you are just writing some simple test code or a simple prototype, you can abuse global space as much as you want since the code will never see the light of day(keyword here is simple). If you are building a simple script that helps generate a couple web pages and that script will never evolve into anything more then that, you still don't have to worry about the global space much. But once you move beyond the trivial, you really should start isolating the parts of the system, as it will give you much better code to work with down the road(meaning pretty much as soon as it is typed in and compiles)

The more complex a project becomes, the more isolated the parts should be, so that a change in one area doesn't 'ripple' through the entire system. This ripple could be looked at as having to change something in the code in a hundred places instead of just a few or in the resulting bugs that come up after the fact because not everything was changed over or changed correctly(Human error will always win out here).

这篇关于是否使用像这样的全局变量来减少对数据库可接受实践的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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