更新后的 PHP: PHP 的新面孔
更新后的 PHP: PHP 的新面孔从早期的模板语言到现在,PHP 显然已经发生了明显的改进。在这个关于当代 PHP 编程的由四个部分组成的系列文章的第一部分中,PHP 专家 Eli White 将介绍 PHP 5.3 及其更高版本中的一些改进。快速了解有关命名空间、特征、闭包、生成器等内容的更多详细信息。
查看本系列更多内容评论:
Eli White, CTO; 大会主席和主编, php
2015 年 5 月 11 日
内容
<?php
namespace zebra;
class DateTime
{
public function __construct() {
echo "Today!";
}
}
function stripes() {
echo "-=-=-=-=-=-=-=-";
}
<?php
include 'listing1.php';
// Use the stripes function I declared in my namespace:
zebra\stripes();
// Use my own DateTime class:
$dt = new zebra\DateTime();
// Now use the 'root' level Datetime class:
$real = new \DateTime('tomorrow noon');
echo $real->format(\DateTime::ATOM);
<?php
include 'listing1.php'; use zebra\DateTime; // Use our own DateTime class:
$dt = new DateTime();
<?php
include 'listing1.php'; use zebra\DateTime as ZDT; // Use our own DateTime class:
$dt = new ZDT();
阅读:php.net 上的命名空间文档
<?php
trait logging {
private static $LOG_ERROR = 100;
private static $LOG_WARNING = 50;
private static $LOG_NOTICE = 10;
protected $log_location = 'output.log';
protected function log($level, $msg) {
$output = [];
$output[] = "Class: ".__CLASS__.' | ';
$output[] = "Level: {$level} | ";
$output = array_merge($output, (array)$msg, ["\n"]);
file_put_contents($this->log_location, $output, FILE_APPEND);
}
}
class User {
use logging;
public function __construct() {
$this->log(self::$LOG_NOTICE, 'New User Created');
}
}
class DB {
use logging;
private $db = 'localhost:8080';
public function connect() {
// ... attempt to connect and fail:
$this->log(self::$LOG_ERROR, ['Connection Failed-', $this->db]);
}
}
阅读:php.net 上的特征文档
<?php
$insurees = [
'u4937' => ['name' => 'Thomas Smythe', 'age' => 33],
'u1282' => ['name' => 'Gayle Runecor', 'age' => 25],
'u9275' => ['name' => 'Sara Pinnicle', 'age' => 57],
'u2078' => ['name' => 'Delilah Shock', 'age' => 41],
];
function insuree_age_sort($a, $b) {
if ($a['age'] == $b['age']) { return 0; }
return ($a['age'] > $b['age']) ? -1 : 1;
}
uasort($insurees, 'insuree_age_sort');
<?php
uasort($insurees, function ($a, $b) {
if ($a['age'] == $b['age']) { return 0; }
return ($a['age'] > $b['age']) ? -1 : 1;
});
<?php
// Find only people over a certain age
$minage = 40;
$over = array_filter($insurees, function($a) use ($minage) {
return ($a['age'] >= $minage);
});
<?php
$urls = [
'training' => '/training',
'magazine' => '/magazine',
't-shirt' => '/swag/tshirts',
];
$current = $_SERVER['REQUEST_URI']; // May come from somewhere else
// Helper for links, ignoring links if we are on that page:
$link = function($name) use ($urls, $current) {
if ($current == $urls[$name]) {
return $name;
} else {
return "<a href=\"{$urls[$name]}\">{$name}</a>";
}
};
?>
<p>Welcome to our website!Make sure to check out
our <?= $link('training') ?> offerings, see the
latest issue of our <?= $link('magazine'); ?>,
and don't forget to check out our latest
<?= $link('t-shirt') ?> designs as well.</p>
阅读:php.net 上的闭包文档
<?php
function parts($start, $end, $parts) {
// Find what our actual length is:
$length = $end - $start;
do {
$start += $length / $parts;
yield $start;
} while ($start < $end);
}
// Break 5 feet into 3 parts:
foreach (parts(0, 5, 3) as $l) {
echo $l, " ";
}
echo "\n";
// Break the range 10-90 into 12 parts:
foreach (parts(10, 90, 12) as $l) {
echo $l, " ";
}
echo "\n";
<?php
$xml = <<<EOXML
<?xml version="1.0" encoding="UTF-8" ?>
<products>
<books>
<book isbn="978-1940111001">Mastering the SPL Library</book>
<book isbn="978-1940111056">Functional Programming in PHP</book>
<book isbn="978-0981034508">Guide to Date and Time Programming</book>
<book isbn="0973589825">Guide to PHP Design Patterns</book>
</books>
</products>
EOXML;
$books = function () use ($xml) {
$products = simplexml_load_string($xml);
foreach ($products->books->book as $book) {
yield $book['isbn'] => (string)$book;
}
};
foreach ($books() as $isbn => $title) {
echo "{$isbn}: {$title}\n";
}
阅读:php.net 上的生成器文档
特性版本描述延迟静态绑定 5.3父类调用一个静态方法或属性的能力已通过其子类/继承类得到定义(与通常采用的方式相反)。例如,允许通用功能存在于从其扩展子类进行配置的某个父类中。Nowdoc 语法 5.3能够指定一个字符串作为一个文本块,其中的任何变量都不会被解释。快捷三元运算符 (?:) 5.3可以省略标准三元运算符的中间部分,这样,跳跃标签 (5.3虽然有些人并不认为可以使用语言来实现 “向前移动”,但随着 神奇的方法
__callStatic
__invoke
5.3
5.3
5.6这三个神奇的新方法被添加到其他可用方法中,在设计您的类时,PHP 5.0 可以完成您可以使用的选项的强大内定功能。现在,您可以使用过载的和未定义的静态方法,像调用函数一样调用对象,并控制调试您的对象时的输出。一直可用的短代码回响 (5.4在以前,如果您在 PHP 中禁用短代码,那么所有变体都将停止使用。自 PHP 5.4 起,短数组语法 5.4现在,您可以使用括号(比如 内置 Web 服务器 5.4PHP 运行时现在提供了一个内置的 Web 服务器,使其能够更轻松地实现简单的代码测试和开发,无需配置 Apache 或 IIS。
本系列的下一个部分将查看针对密码保护的不断变化的需求领域,PHP 一直都在帮助 Web 开发人员处理这个复杂的需求。
http://www.ibm.com/developerworks/cn/web/wa-php-renewed_1/#ibm-pcon
页:
[1]