您的位置:首页 > Web前端

Language Constructs和Functions的区别(The Difference Between Functions & Language Constructs)

2009-12-29 15:17 686 查看
Persons new to programming often have difficulty grasping the concept of language constructs. Furthermore, both new programmers and seasoned programmers often get tripped up when it comes to telling the difference between language constructs and functions as they often resemble each other and in many instances are used in the same way. As a result, many developers write code for a number of years mistaking language constructs for functions and only become aware of the difference after trying to fix some odd quirk in their applications which arises because of a misunderstanding of the fundamental differences between the two.

Functions

Essentially, a function is a block of code which is written in such a way that it may be used and reused multiple times in the execution of a script. It may be designed to accept arguments and return values or it may do neither. If it is designed to take arguments, they will invariably affect the procedures of the function and, quite likely, any value(s) returned by the function. In PHP, functions may be compiled with the language (referred to as core or native functions), they may be user defined (i.e. created by the programmer writing the script) or they may be accessed as components of external libraries or extensions available via PECL. Examples of PHP functions include:

json_encode()

mysql_connect()

preg_replace()

strtolower()

array_shift()

unlink()

mail()

ob_start()

file_get_contents()

curl_init()

Language Constructs

Language constructs are keywords that are a part of the syntax of the language, i.e. they are parts of the language itself. They cannot be user defined nor can they added to the language via extensions or libraries. They may or may not take arguments and they may or may not have return values (although most of them don’t). Examples of language constructs in PHP include:

echo()

print()

die()

include()

require()

empty()

isset()

unset()

list()

array()

Note that some language constructs do not require the use of parentheses:

print('one');
//works in the same way as
print 'one';
//the above code outputs 'oneone' and
include('file.php');
//works in the same way as
include 'file.php';


The Difference

The key difference between functions and language constructs is that language constructs are the most basic units of the language and cannot be broken down further by the PHP parser whereas functions have to be further broken down before being parsed, ofetn into language constructs. In other words, in just the same way that PHP code has to be broken down into lower level opcode by the PHP parser in order for the machine to understand it, functions must be broken down to language constructs by the PHP parser before they are parsed. There are a few interesting consequences of this:

Language constructs tend to be faster than their function counterparts.

Language constructs in some cases may be able to bypass error handling mechanisms.

While functions can be disabled in PHP via the configuration file, language constructs cannot.

Language constructs cannot be used as callback functions.

Happy coding!

Most languages are made up of something called a "syntax": the language is comprised of several well-defined keywords, and the complete range of expressions that you can construct in that language is built up from that syntax.

For example, let's say you have a simple four-function arithmetic "language" that only takes single-digit integers as input and completely ignores order of operations (I told you it was a simple language). That language could be defined by the syntax:

// The | means "or" and the := represents definition
$expression := $number | $expression $operator $expression
$number := 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
$operator := + | - | * | /

From these three rules, you can build any number of single-digit-input arithmetic expressions. You can then write a parser for this syntax that breaks down any valid input into its component types (
$expression
,
$number
, or
$operator
) and deals with the result. For example, the expression
3 + 4 * 5
can be broken down as follows:

// Parentheses used for ease of explanation; they have no true syntactical meaning
$expression = 3 + 4 * 5
= $expression $operator (4 * 5) // Expand into $exp $op $exp
= $number $operator $expression // Rewrite: $exp -> $num
= $number $operator $expression $operator $expression // Expand again
= $number $operator $number $operator $number // Rewrite again

Now we have a fully parsed syntax, in our defined language, for the original expression. Once we have this, we can go through and write a parser to find the results of all the combinations of
$number $operator $number
, and spit out a result when we only have one
$number
left.

Take note that there are no
$expression
constructs left in the final parsed version of our original expression. That's because
$expression
can always be reduced to a combination of other things in our language.

PHP is much the same: language constructs are recognized as the equivalent of our
$number
or
$operator
. They cannot be reduced into other language constructs; instead, they're the base units from which the language is built up. The key difference between functions and language constructs is this: the parser deals directly with language constructs. It simplifies functions into language constructs.

The reason that language constructs may or may not require parentheses and the reason some have return values while others don't depends entirely on the specific technical details of the PHP parser implementation. I'm not that well-versed in how the parser works, so I can't address these questions specifically, but imagine for a second a language that starts with this:

$expression := ($expression) | ...

Effectively, this language is free to take any expressions it finds and get rid of the surrounding parentheses. PHP (and here I'm employing pure guesswork) may employ something similar for its language constructs:
print("Hello")
might get reduced down to
print "Hello"
before it's parsed, or vice-versa (language definitions can add parentheses as well as get rid of them).

This is the root of why you can't redefine language constructs like
echo
or
print
: they're effectively hardcoded into the parser, whereas functions are mapped to a set of language constructs and the parser allows you to change that mapping at compile- or runtime to substitute your own set of language constructs or expressions.

At the end of the day, the internal difference between constructs and expressions is this: language constructs are understood and dealt with by the parser. Built-in functions, while provided by the language, are mapped and simplified to a set of language constructs before parsing.

More info:

Backus-Naur form, the syntax used to define formal languages (yacc uses this form)

Edit: Reading through some of the other answers, people make good points. Among them:

A language builtin is faster to call than a function. This is true, if only marginally, because the PHP interpreter doesn't need to map that function to its language-builtin equivalents before parsing. On a modern machine, though, the difference is fairly negligible.

A language builtin bypasses error-checking. This may or may not be true, depending on the PHP internal implementation for each builtin. It is certainly true that more often than not, functions will have more advanced error-checking and other functionality that builtins don't.

Language constructs can't be used as function callbacks. This is true, because a construct is not a function. They're separate entities. When you code a builtin, you're not coding a function that takes arguments - the syntax of the builtin is handled directly by the parser, and is recognized as a builtin, rather than a function. (This may be easier to understand if you consider languages with first-class functions: effectively, you can pass functions around as objects. You can't do that with builtins.)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐