博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP 5.6的新功能
阅读量:2516 次
发布时间:2019-05-11

本文共 9795 字,大约阅读时间需要 32 分钟。

It’s no blasphemy saying the core devs of PHP have had some hiccups and some truly ridiculous arguments about some of the features – just look at on why a shorthand array syntax is bad, back from ten years ago. Arguments like these, among other things, make people think some of the core devs don’t even use PHP in their day to day lives, and are why the devs of PHP and the people using PHP are often considered unprofessional.

毫不夸张地说,PHP的核心开发人员对某些功能有一些打and和一些真正荒谬的论点–只需回顾一下十年前关于速记数组语法为何不好的 。 诸如此类的争论使人们认为某些核心开发人员甚至在日常生活中甚至没有使用PHP,这就是为什么PHP的开发人员和使用PHP的人们通常被认为不专业的原因。

The future isn’t bleak, though. It’s been a while since the release of PHP 5.4, and new versions have been coming out faster and faster ever since. When 5.5 hit and introduced some unexpectedly great features, the PHP community breathed a sigh of relief and regained hope of a more dedicated, structured and smart core development. Whether or not we’ll actually get this remains to be seen, but the future does indeed look promising, especially if one looks at the made so far.

不过,未来并不暗淡。 自PHP 5.4发行以来已经有一段时间了,自那时以来,新版本的发布越来越快。 当5.5发行并引入了一些出乎意料的出色功能时,PHP社区松了一口气,并重新获得了更加专注,结构化和智能化的内核开发的希望。 我们是否会真正看到这一点还有待观察,但是未来的前景确实是充满希望的,特别是如果人们看看到目前为止所做的 。

While a full explanation of all the upcoming updates would be far too vast to cover in one article, I would like to direct your attention at some I personally deem most important.

尽管对所有即将到来的更新的完整解释太过庞大而无法在一篇文章中进行介绍,但我想将您的注意力吸引到我个人认为最重要的一些方面。

CLI Web服务器中的MIME类型 (MIME types in the CLI web server)

MIME types in PHP can be used to output content as a type other than PHP, that is, as a type other than text/html. When you run a PHP page, the default output is text/html, but you can use headers to set it as, for example, PDF and generate PDF files. When a server is aware of different MIME types, as most servers like HHVM, Apache and Nginx usually are, they know how to serve a given file by default, judging by its extension, without you having to set specific instructions in PHP itself. The command line server from PHP 5.4 had only a few MIME types so far, and this version will introduce dozens more. It’s safe to say that all the common MIME types will be covered by the built in PHP server now.

PHP中的MIME类型可用于将内容输出为PHP以外的其他类型,即text / html以外的其他类型。 运行PHP页面时,默认输出为text / html,但是您可以使用标头将其设置为PDF,并生成PDF文件。 当服务器知道不同的MIME类型时,就像大多数服务器(例如HHVM,Apache和Nginx)通常所知道的那样,它们知道如何根据给定的文件扩展名默认情况下为给定文件提供服务,而无需在PHP本身中设置特定的说明。 到目前为止,PHP 5.4的命令行服务器只有几种MIME类型,并且此版本将引入更多的MIME类型。 可以肯定地说,所有内置PHP服务器现在都将覆盖所有常见的MIME类型。

内部操作员超载 (Internal Operator Overloading)

This is a feature we as web developers using PHP probably won’t be exposed to, due to the keyword “internal”. Internal means “non userland” where userland is the area of PHP development we, the end users of PHP, use. It applies only to internal classes, in order to make development in that area simpler and code cleaner to read. A detailed explanation can be found .

作为使用PHP的Web开发人员,由于关键字“ internal”,我们可能不会接触到此功能。 内部表示“非用户态”,其中用户态是我们(PHP的最终用户)使用PHP开发领域。 它仅适用于内部类,以使该领域的开发更简单且代码更易于阅读。 在可以找到详细的解释。

现在接受超过2GB的上传 (Uploads of over 2GB are now accepted)

Until 5.6, no uploads of 2GB and over were supported in PHP. This is no longer the case, as the changelog states, and uploads of arbitrary size are now supported.

在5.6之前,PHP不支持2GB以上的上传。 情况已不再如此,因为更改日志状态以及现在支持任意大小的上载。

POST数据内存使用量减少 (POST data memory usage decreased)

POST data memory usage has been shrunk by 2 to 3 times, following two removals: the always_populate_raw_post_data setting from php.ini, and the superglobal variable $HTTP_RAW_POST_DATA. What this means is you can no longer access raw post data that way, but need to rely on a solution such as:

两次删除之后,POST数据内存使用量减少了2到3倍: php.inialways_populate_raw_post_data设置和超全局变量$HTTP_RAW_POST_DATA 。 这意味着您不能再以这种方式访问​​原始帖子数据,而是需要依靠以下解决方案:

$postdata = file_get_contents("php://input");

Note that getting POST via ://input is unavailable when a form is multipart (in other words, when a form has a file upload element).

请注意,当表单多部分时(换句话说,当表单具有文件上载元素时),无法通过://input获取POST。

可变参数函数的改进语法 (Improved syntax for variadic functions)

Variadic functions are functions which can take an arbitrary amount of arguments. When you supplied some arguments to it, you usually had to do some splicing after calling func_get_args, which was somewhat impractical. As taken from example , the syntax in 5.5 and earlier was:

可变参数函数是可以接受任意数量参数的函数。 当提供一些参数时,通常在调用func_get_args之后必须进行一些拼接,这是不切实际的。 自实例采取 ,语法在5.5和更早的版本是:

class MySQL implements DB {    protected $pdo;    public function query($query) {        $stmt = $this->pdo->prepare($query);        $stmt->execute(array_slice(func_get_args(), 1));        return $stmt;    }    // ...}$userData = $db->query('SELECT * FROM users WHERE id = ?', $userID)->fetch();

now, the syntax will be:

现在,语法将是:

class MySQL implements DB {    public function query($query, ...$params) {        $stmt = $this->pdo->prepare($query);        $stmt->execute($params);        return $stmt;    }    // ...}

As you can see, the ...$params syntax tells the function to accept the first parameter as is, and to put all the others into the $params array. This rids us of the splicing and calling func_get_params, improves function signatures, and makes the code more readable.

如您所见, ...$params语法告诉函数按原样接受第一个参数,并将所有其他参数放入$ params数组中。 这使我们摆脱了拼接和调用func_get_params ,改进了函数签名,并使代码更具可读性。

The new syntax also allows passing of extra arguments by reference, by prefixing ...$params with an ampersand, like so: &...$params. This was not possible before with func_get_args.

新语法还允许在引用中传递额外的参数,方法是在...$params前面加上一个&符号,例如: &...$params 。 以前不可能通过func_get_args

参数解包 (Argument unpacking)

Note: thanks to for pointing out this feature – it got implemented around the time of this article’s original writing

注意:感谢指出了此功能-该功能是在本文最初撰写时实现的

Following improved support for variadic functions, got approved, too.

随着对可变参数函数的支持得到改善, 也得到了批准。

Until now, the only way to call a function with an arbitrary number of arguments passed in as params was by using meaning literally “call userland function with array of params”. This was clumsy and awkward, wasn’t supported in constructors, was slow, and required a callback in the form of a string – a function name – which means no IDE support in most cases.

到目前为止,调用带有作为参数传入的任意数量参数的函数的唯一方法是使用含义实际上是“调用带有参数数组的userland函数”。 这笨拙且笨拙,在构造函数中不受支持,运行缓慢,并且需要以字符串形式(函数名)进行回调,这意味着在大多数情况下不支持IDE。

Unpacking would eliminate all the downsides of said function, and naturally complements the variadic support seen above. Unpacking works like so:

拆包将消除上述功能的所有弊端,并自然补充上述的可变参数支持。 打开包装的方式如下:

$args = [1, 3, 5, 7, 9];MyClass::someMethod(...$args);

This is the same as calling

这跟打电话一样

MyClass::someMethod(1, 3, 5, 7, 9);

i.e. passing in the arguments in one by one. This works in any concievable scenario, from class constructors to being called multiple times in a call, anything goes. See the RFC linked above for usage examples and further explanations.

即,将参数一一传递。 从类构造器到调用中多次调用,此方法在任何可行的方案中均有效。 有关用法示例和进一步的说明,请参见上面链接的RFC。

常数标量表达式 (Constant Scalar Expressions)

This added the ability to have expressions in places that only expect static values. What this means is you can now have basic arithmetic and logical structures in constant declarations, functions arguments, class properties etc.

该增加了在仅需要静态值的位置具有表达式的功能。 这意味着您现在可以在常量声明,函数参数,类属性等中拥有基本的算术和逻辑结构。

For example, previously, code like this would throw an error:

例如,以前,这样的代码将引发错误:

const a = 1;const b = a?2:100;

Now, this is no longer the case.

现在,情况已不再如此。

One might argue whether a constant really is constant if it depends on the value of another constant, but such meta discussions are better left for other times.

有人可能会争论一个常数,如果它真的取决于另一个常数的值,则它是否真的是常数,但是这种元讨论最好留给其他时间。

PHPDBG默认捆绑 (PHPDBG bundled by default)

The gdb-like debugger, phpdbg, is now bundled by default as . It’s used from the command line, or a simplistic Java UI, specifying break points interactively, altering programs at runtime and more. It can also inspect Opcode, and be used from withinyour PHP code. Learn more about phpdbg .

类似于gdb的调试器phpdbg现在默认捆绑为 。 它可从命令行或简单的Java UI中使用,以交互方式指定断点,并在运行时更改程序等。 它还可以检查操作码,并可以在您PHP代码中使用。 了解有关phpdbg的更多信息。

拉链改进 (Zip improved)

The Zip library got several improvements, particularly in the form of new methods. One that stands out especially is ZipArchive::setPassword($password) which finally allows you to easily create password protected Zip files.

Zip库得到了一些改进,尤其是以新方法的形式。 ZipArchive::setPassword($password)特别引人注目,它最终使您可以轻松创建受密码保护的Zip文件。

导入命名空间函数 (Importing namespaced functions)

As per , the new version will allow importing of namespaced functions and constants. Right now we are able to import namespaces and types (classes/interfaces/traits) via the use statement, like so:

根据 ,新版本将允许导入命名空间的函数和常量。 现在,我们能够通过use语句导入名称空间和类型(类/接口/特征),如下所示:

namespace foo\bar {    function baz() {        return 'foo.bar.baz';    }}namespace {    use foo\bar as b;    var_dump(b\baz());}

From 5.6, we’ll be able to use the use function and use const statements to import a lone function or constant (even class constant).

从5.6开始,我们将能够使用use functionuse const语句导入一个单独的函数或常量(甚至是类常量)。

namespace {    use function foo\bar as foo_bar;    use const foo\BAZ as FOO_BAZ;    var_dump(foo_bar());    var_dump(FOO_BAZ);}

结论 (Conclusion)

PHP 5.6, which as of this moment still doesn’t have a release date, definitely looks promising, and hopefully this short overview of the changelog helped you understand how important it will be for you to upgrade as soon as it’s out, if at all. For the remainder of the upgrades, please see the file, and keep checking back for updates. If I’ve missed something important, or misinterpreted something, please let me know in the comments below.

PHP 5.6(到目前为止还没有发布日期)绝对看起来很有希望,希望变更日志的简短概述可以帮助您了解,一旦发布,升级对您的重要性(如果有的话) 。 对于其余的升级,请参阅文件,并继续检查更新。 如果我错过了重要的事情,或误解了一些事情,请在下面的评论中告诉我。

翻译自:

转载地址:http://ixrgb.baihongyu.com/

你可能感兴趣的文章
leetcode375 Guess Number Higher or Lower II
查看>>
docker 创建基础镜像
查看>>
移动端-新建空白页面meta各个标签详细解读
查看>>
算法系列之一 快速排序
查看>>
状压DP【p1896】[SCOI2005]互不侵犯
查看>>
gym 101982 B题 Coprime Integers
查看>>
Polynomial Division 数学题
查看>>
JAVA 第四周学习总结
查看>>
JS学习记录(数组)
查看>>
数据可视化六步法
查看>>
C++中的二级指针和指针引用函数传参
查看>>
Beam编程系列之Python SDK Quickstart(官网的推荐步骤)
查看>>
Fluent Ribbon 第四步 快速启动栏
查看>>
【Linux基础】查看硬件信息-硬盘
查看>>
C#通过WebClient/HttpWebRequest实现http的post/get方法
查看>>
tomcat服务器
查看>>
GridView导出到excel
查看>>
201511141852-《NserviceBus》
查看>>
linux文件操作
查看>>
【BZOJ-2295】我爱你啊 暴力
查看>>