为什么常量在定义时引用并且类引号没有引号? [英] Why do constants have quotes around when defined and class quoteshave no quotes??

查看:99
本文介绍了为什么常量在定义时引用并且类引号没有引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些知道

PHP的人来说,这是一个非常基本的问题。这不是问题,只是我很困惑的事情。我在阅读下面这篇文章时想知道为什么正常常数

设置了它们周围的引号,而类常量则是这样。 (那些我昨天从文章中学到的东西很少b $ b)当你定义它们时,不必有引号

围绕常数?对我来说这似乎很奇怪

为什么PHP会这样做?


常数: http://www.talkphp.com/showthread.php?t=1151


有人可以向我解释为什么有两种方法来定义它们 - 一个通过一个函数,一个通过,它是什么?一种语言

构造?我不知道。后者没有报价

定义!!真的很奇怪。

This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I''m confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don''t have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??

Constants: http://www.talkphp.com/showthread.php?t=1151

Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don''t know. The latter having no quotes around to
define!! Really weird.

推荐答案

12月11日14:00,adam.timberl ... @ gmail.com写道:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:

对于所有知道

PHP的人来说,这是一个非常基本的问题。这不是问题,只是我很困惑的事情。我在阅读下面这篇文章时想知道为什么正常常数

设置了它们周围的引号,而类常量则是这样。 (那些我昨天从文章中学到的东西很少b $ b)当你定义它们时,不必有引号

围绕常数?对我来说这似乎很奇怪

为什么PHP会这样做?


常数: http://www.talkphp.com/showthread.php?t=1151


有人可以向我解释为什么有两种方法来定义它们 - 一个通过一个函数,一个通过,它是什么?一种语言

构造?我不知道。后者没有报价

定义!!真的很奇怪。
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I''m confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don''t have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??

Constants:http://www.talkphp.com/showthread.php?t=1151

Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don''t know. The latter having no quotes around to
define!! Really weird.



就我所见,文章告诉你。


一种方法定义应用全局常量另一种方法;

只对全球定义为 (或者更恰当地说仅仅是对于定义它的类来说是全局的b $ b。。

The article tells you as far as I can see.

One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").


12月11日,14:22 ,Paralytic上尉< paul_laut ... @ yahoo.comwrote:
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:

12月11日14:00,adam.timberl ... @ gmail.com写道:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:

对于那些知道

PHP的人来说,这是一个非常基本的问题。这不是问题,只是我很困惑的事情。我在阅读下面这篇文章时想知道为什么正常常数

设置了它们周围的引号,而类常量则是这样。 (那些我昨天从文章中学到的东西很少b $ b)当你定义它们时,不必有引号

围绕常数?对我来说这似乎很奇怪

为什么PHP会这样做?
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I''m confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don''t have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??


常数: http://www.talkphp.com/showthread.php?t=1151


有人可以向我解释一下为什么有两种方法来定义它们 - 一个通过一个函数,一个通过,它是什么?一种语言

构造?我不知道。后者没有报价

定义!!真的很奇怪。
Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don''t know. The latter having no quotes around to
define!! Really weird.



就我所见,这篇文章告诉你。


一种方法定义应用全局常量另一种方法;

只对全球定义为 (或者更恰当地说仅仅是对于定义它的类来说是全局的b $ b)。


The article tells you as far as I can see.

One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").



我明白怎么做,我只是不明白为什么?为什么没有引号定义一个

??

I understand how to do it, I just don''t understand why?? Why is one
defined without quotes??


12月11日上午9:29,adam.timberl ... @ gmail.com写道:
On Dec 11, 9:29 am, adam.timberl...@gmail.com wrote:

12月11日14:22,Paralytic上尉< paul_laut ... @ yahoo.comwrote:
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:

12月11日14:00,adam.timberl ... @ gmail.com写道:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:


对于所有知道

PHP的人来说,这是一个非常基本的问题。这不是问题,只是我很困惑的事情。我在阅读下面这篇文章时想知道为什么正常常数

设置了它们周围的引号,而类常量则是这样。 (那些我昨天从文章中学到的东西很少b $ b)当你定义它们时,不必有引号

围绕常数?对我来说这似乎很奇怪

为什么PHP会这样做?
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I''m confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don''t have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??


常数: http://www.talkphp.com/showthread.php?t=1151


有些人可以向我解释为什么有两种方法来定义它们 - 一个通过一个函数,一个通过, 它是什么?一种语言

构造?我不知道。后者没有报价

定义!!真的很奇怪。
Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don''t know. The latter having no quotes around to
define!! Really weird.


就我所见,文章告诉你。
The article tells you as far as I can see.


一种方法定义了一个应用全局常量,另一种方法是只有全局到它定义的类 ; (或者更恰当地说仅仅是对于定义它的类来说是全局的b $ b)。
One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").



我明白怎么做,我只是不明白为什么?为什么一个

定义没有引号?


I understand how to do it, I just don''t understand why?? Why is one
defined without quotes??



因为当你这样做时:


define(''FOO'',''BAR'');


你正在调用一个函数。该函数接受参数(即

字符串和其他内容)。必须引用文字字符串。


如果是类常数:


class foo {

const BAR =''BAZ'';

}


你没有调用函数 - 它是一种语言结构。事实上你没有在BAR中引用引用的事实就是PHP的设计者选择实现它的方式。

Because when you do this:

define(''FOO'', ''BAR'');

you''re calling a function. That function takes parameters (namely a
string and something else). Literal strings must be quoted.

In the case of a class constant:

class foo {
const BAR = ''BAZ'';
}

you''re not calling a function -- it''s a language construct. The fact
that you don''t put quotes around the BAR is simply the way the
designers of PHP chose to implement it.


这篇关于为什么常量在定义时引用并且类引号没有引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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