Modern PHP笔记
来源:互联网 发布:考研英语网络课 编辑:程序博客网 时间:2024/06/07 00:09
1.Namespace
namespace Oreilly;namespace Oreilly/ModernPHP;
2.Import and Alias
use Symfony\Component\HttpFoundation\Response;$response = new Response('Oops',400);$response->send();
or
use Symfony\Component\HttpFoundation\Response as Res;$response = new Res('Oops',400);$response->send();
As of PHP 5.6, it’s possible to import functions and constants.
use func Namespace\functionName;functionName();
use constant Namespace\CONST_NAME;echo CONST_NAME;
global namespace
namespace My\App;class Foo{ public function doSomething() { throw new \Exception(); }}
3.Code to Interface
interface Documentable{ public function getId(); public function getContent();}class HtmlDocument implements Documentable{//implement the interface...}
4.Traits
Has in PHP 5.4, a trait is a partial class implementation that can be mixed into one or more existing PHP classes. Traits work double duty: they say what a class can do(like an interface), and the provide a modular implementation(like a class).
Define a trait
trait Geocodable{ // define variables and methods }
Use a trait
class MyClass{ use Geocodable;}
5.Generator
function MakeRange($length){ for($i = 0;$i<$length;$i++){ yield $i; }}foreach(MakeRange(1000000) as $i){ echo $i,PHP_EOL;}
6.Closures
$closure = function ($name){ return sprintf('Hello %s',$name);};echo $closure("Josh");// Hello Josh
or
$numbersPlusOne = array_map(function ($number){ return $number+1;},[1,2,3]);print_r($numbersPlusOne);// [2,3,4]
attach state
attach state to a PHP closure with the closure object’s bindTo() method or the use keyword.
function enclosePerson($name){ return function ($doCommand) use ($name){ return sprintf('%s,%s',$name,$doCommand); };}$clay = enclosePerson('Clay');echo $clay('get me sweet tea!'); // Clay, get me sweet tea!
0 0
- Modern PHP笔记
- Modern PHP
- Modern PHP
- Modern PHP读书笔记一
- Effective Modern C++(笔记)
- modern c++ design Int2Type 笔记
- modern C++ design type2type 笔记
- modern c++ design Int2Type 笔记
- unique_ptr, effective modern c++笔记
- Effective Modern C++笔记汇总
- Modern C++ Design 笔记 第十章 Visitor
- 《Modern Information Retrieval》笔记(二)
- 《Modern Information Retrieval》笔记(一)
- 《modern operating system》 chapter 6 DEADLOCKS 笔记
- Learning Modern 3D Graphics Programming笔记
- Extjs6 modern安卓开发笔记
- Modern JavaScript Applications(阅读笔记)
- 《Modern Cryptogrophy:Theory and practice》学习笔记之第一章
- R语言笔记--从矩阵的操作说起
- ServletContext 读取Web应用中的资源文件
- LeetCode447. Number of Boomerangs
- 解决iframe跳转不刷新整个页面小问题
- MATLAB随机函数
- Modern PHP笔记
- 大整数排序
- 编写gSoap XML/RPC程序时出现“soapStub.h: error: redefinition of parameter ‘soap’”
- Connection to localhost:5432 refused. Check that the hostname and port are correct and that the post
- Linux下使用popen()执行shell命令
- 《HTTP权威指南》读书笔记(一):初探缓存
- 二叉树的后序遍历
- 动手写window程序
- @Retention注解