必须在一个类中使用全局变量 [英] One must use globals in ones classes

查看:61
本文介绍了必须在一个类中使用全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的印象是,大多数在他们的

PHP项目中使用对象的人都将它们与程序代码混合在一起。设计,我认为,程序代码是客户端代码,

类是实用程序代码库。

因此,当我询问如何将全局变量转换为对象时,我已经告知我应该将它们作为参数传递给方法。

如果你有一些程序代码就够了。这个答案我已经给出了b $ b假定在对象之外有一些东西,一些可以抓住全局的程序代码,然后把它交给<对象。
对象。


但是,如果你想要纯粹的OO,那我就错了,你要

必须从对象内部抓取全局变量?您可以将

限制为构造函数,并且可能限制为仅包含所有其他对象的单个

主对象,但是,您仍然可以

直接将全局拉入对象,是吗?

I get the impression that most people who are using objects in their
PHP projects are mixing them with procedural code. The design, I
think, is one where the procedural code is the client code, and the
classes are a library of utility code.

As such, when I''ve asked about how to get globals into objects, I''ve
been told that I should pass them in as the parameters to a method.
Easy enough if you have some procedural code. This answer I''ve been
given assumes that there is something outside of the object, some
procedural code that can get hold of the global and then hand it to
the object.

However, am I wrong in saying that if you wanted to go pure OO, you''d
have to grab the globals from inside of the objects? You could limit
this to the constructor, and possibly you could limit to just a single
master object that contained all other objects, but still, you''d have
to do pull globals straight into the object, yes?

推荐答案

lawrence写道:
lawrence wrote:
但是,我说错了,如果你想要纯粹的OO,你是否必须从对象内部抓取全局变量?您可以将
限制为构造函数,并且可能只限制一个包含所有其他对象的主对象,但是,您仍然可以直接执行拉全局变量进入对象,是吗?
However, am I wrong in saying that if you wanted to go pure OO, you''d
have to grab the globals from inside of the objects? You could limit
this to the constructor, and possibly you could limit to just a single
master object that contained all other objects, but still, you''d have
to do pull globals straight into the object, yes?




基本上,你想要将全局变量的使用限制为最小值,全局变量

是坏mmkay,从全局到goto只需一小步:-)


如果绝对必须使用全局变量,最好的方法是使用



Basically, you want to limit the use of globals to bare-minimums, globals
are bad mmkay, just one small step from globals to goto :-)

If you absolutely must use globals, best way would be to use


GLOBAL [''varname'']就是这样,你可以在全球范围内访问某些东西。


- < br $>
James Sleeman

Gogo:代码, http://www.gogo.co.nz/

PHP& Coldfusion编程服务

电子邮件域名:gogo.co.nz从标题中查看用户!
GLOBAL[''varname''] that way there is no mistaking the fact that you are
accessing something in the global scope.

--
James Sleeman
Gogo:Code, http://www.gogo.co.nz/
PHP & Coldfusion Programming Services
Email domain : gogo.co.nz see user in from header!


嗨劳伦斯!

2003年7月14日13:20:24 -0700, lk******@geocities.com (劳伦斯)

写道:
Hi Lawrence!
On 14 Jul 2003 13:20:24 -0700, lk******@geocities.com (lawrence)
wrote:
我得到的印象是大多数在他们的PHP项目中使用对象的人都将它们与程序代码混合在一起。我认为,设计是一个程序代码是客户端代码的设计,而
类是一个实用程序代码库。

因此,当我''我问过如何将全局变量转换为对象,我已经被告知我应该将它们作为参数传递给方法。
如果你有一些程序代码就足够了。我已经给出了这个答案,假设在对象外面有一些东西,一些可以抓住全局的程序代码,然后把它交给对象。但是,我说错了,如果你想要纯粹的OO,你是否必须从对象内部抓取全局变量?您可以将
限制为构造函数,并且可能只限制一个包含所有其他对象的主对象,但是,您仍然可以直接执行拉全局变量进入对象,是吗?
I get the impression that most people who are using objects in their
PHP projects are mixing them with procedural code. The design, I
think, is one where the procedural code is the client code, and the
classes are a library of utility code.

As such, when I''ve asked about how to get globals into objects, I''ve
been told that I should pass them in as the parameters to a method.
Easy enough if you have some procedural code. This answer I''ve been
given assumes that there is something outside of the object, some
procedural code that can get hold of the global and then hand it to
the object.

However, am I wrong in saying that if you wanted to go pure OO, you''d
have to grab the globals from inside of the objects? You could limit
this to the constructor, and possibly you could limit to just a single
master object that contained all other objects, but still, you''d have
to do pull globals straight into the object, yes?




是和否。


如果将变量作为参数传递,则可以控制范围

变量很好,但你的类的接口变得臃肿。这个

使得很难改变它们并且难以测试函数。


如果使用全局变量,则无法控制范围,即。你可以

在另一个函数中覆盖另一个变量,因为它是全局的



我使用的解决方案是一个中介对象,这是一个合适的OO解决方案(参见Gamma的模式书)

- 将全局变量的控制集中到一个类中。我用
使用它来创建一个对象,这样我就可以共享相同的

信息而不会创建任何不必要的对象


调解员对象是这样的:


类调解员{


函数& object_instance(){


static



Yes and no.

If you pass the variables as parameters, you control the scope of the
variables well, but the interfaces to your class get bloated. This
makes it hard to change them and hard to test the functions.

If you use globals, you have no control of the scope, ie. you may
overwrite a different variable in another function, because it is
global.

A solution I use is a mediator object, which is
- a proper OO solution (see the Patterns book by Gamma)
- centralizes the control of the global variables into one class. I
use this to create only one object, which allows me to share the same
information and not create any unnecessary objects

A mediator object goes like this:

class mediator{

function &object_instance(){

static


这篇关于必须在一个类中使用全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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