监理公司管理系统 | 工程企业管理系统 | OA系统 | ERP系统 | 造价咨询管理系统 | 工程设计管理系统 | 签约案例 | 购买价格 | 在线试用 | 手机APP | 产品资料
X 关闭
南昌网站建设公司

当前位置:工程项目OA系统 > 泛普各地 > 江西OA系统 > 南昌OA系统 > 南昌网站建设公司

PHP 5.4 相关知识

申请免费试用、咨询电话:400-8352-114

PHP 5.4来了,这是自5.3后的又一次主版本晋级。此次晋级改动较为明显,删除了一些过气儿的函数,带来了高达20%的速度提拔和更少的内存运用。

  新特征与改动

  此次更新的要害新特征,包罗:新增traits,更精简的Array数组语法,供测试运用的内建webserver,可以闭包运用的$this指针,实例化类成员拜访,

  PHP 5.4.0 功能大幅提拔, 修复超越100个bug. 废弃了register_globals, magic_quotes以及平安形式。 别的值得一提的是多字节支撑曾经默许启用了,default_charset从ISO-8859-1曾经变为UTF-8. 默许发送“Content-Type: text/html; charset=utf-8”,你再也不需求在HTML里写meta tag,也无需为UTF-8兼容而传送额定的header了。

  Traits

  Traits (横向重用/多重承继)是一组构造很像“类”(但不克不及实例化)的办法,它可以闪开发人员在分歧的类中轻松地重用办法。 PHP为单承继言语,子类只能承继一个父类,于是Traits来了。

  Traits的最佳使用是多类之间可以共享一样的函数。打个比如,我们要做个网站,需求运用Facebook和Twitter的APIs。我们要建 2个类,假如是以前,我们需求写一个cURL的办法而且复制/粘贴到两个类中。目前不必了,运用Traits重用代码吧,此次真正地遵照了 DRY(Don’t Repeat Yourself)准则。

  <span style="COLOR: rgb(0,100,0)">/** cURL wrapper trait */

<span style="COLOR: rgb(0,100,0)">  trait cURL

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  public function curl($url)

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  $ch = curl_init();

<span style="COLOR: rgb(0,100,0)">  curl_setopt($ch, CURLOPT_URL, $url);

<span style="COLOR: rgb(0,100,0)">  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

<span style="COLOR: rgb(0,100,0)">  $output = curl_exec($ch);

<span style="COLOR: rgb(0,100,0)">  curl_close($ch);

<span style="COLOR: rgb(0,100,0)">  return $output;

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  /** Twitter API Class */

<span style="COLOR: rgb(0,100,0)">  class Twitter_API

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  use cURL; // use trait here

<span style="COLOR: rgb(0,100,0)">  public function get($url)

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  return json_decode($this->curl('http://api.twitter.com/'.$url));

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  /** Facebook API Class */

<span style="COLOR: rgb(0,100,0)">  class Facebook_API

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  use cURL; // and here

<span style="COLOR: rgb(0,100,0)">  public function get($url)

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  return json_decode($this->curl('http://graph.facebook.com/'.$url));

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  $facebook = new Facebook_API();

<span style="COLOR: rgb(0,100,0)">  echo $facebook->get('500058753')->name; // Rasmus Lerdorf

<span style="COLOR: rgb(0,100,0)">  /** Now demonstrating the awesomeness of PHP 5.4 syntax */

<span style="COLOR: rgb(0,100,0)">  echo (new Facebook_API)->get('500058753')->name;

<span style="COLOR: rgb(0,100,0)">  $foo = 'get';

<span style="COLOR: rgb(0,100,0)">  echo (new Facebook_API)->$foo('500058753')->name;

<span style="COLOR: rgb(0,100,0)">  echo (new Twitter_API)->get('1/users/show.json?screen_name=rasmus')->name;

  看清楚了吗?没有?那你来瞅瞅更简略的例子

  <span style="COLOR: rgb(0,100,0)">trait Hello

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  public function hello()

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  return 'Hello';

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  trait Cichui

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  public function cichui()

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  return ' cichui';

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  class HelloCichui

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  use Hello, Cichui;

<span style="COLOR: rgb(0,100,0)">  public function the_end()

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  return '!';

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  $o = new HelloCichui;

<span style="COLOR: rgb(0,100,0)">  echo $o->hello(), $o->cichui(), $o->the_end();

<span style="COLOR: rgb(0,100,0)">  echo (new Hello)->hello(), (new Cichui)->cichui(), (new HelloCichui)->the_end();

  内建的Web-Sever

  在Web开拓中,Apache HTTPD是PHP的最佳拍档。有时,你开拓时用不上需求装备httpd.conf的apache大杀器,而只需求一个可以在敕令行中运用的超小型 Webserver. 感激PHP(先感激国度),PHP 5.4此次内建了CLI Web server。(PHP CLI webserver仅供开拓运用,回绝产物用处)

  举个栗子(windows平台):

  步调一:树立web根目次, Router和Index

  在硬盘根目次(比方C盘)树立一个public_html目次,目次里新建一个router.php文件,把以下代码复制粘贴进去:

 <span style="COLOR: rgb(0,100,0)">  // router.php

<span style="COLOR: rgb(0,100,0)">  if (preg_match('#\.php$#', $_SERVER['REQUEST_URI']))

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  require basename($_SERVER['REQUEST_URI']); // serve php file

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  else if (strpos($_SERVER['REQUEST_URI'], '.') !== false)

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  return false; // serve file as-is

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  ?>

  再来新建一个index.php文件,复制粘贴以下代码:

<span style="COLOR: rgb(0,100,0)">  // index.php

<span style="COLOR: rgb(0,100,0)">  echo 'Hello cichui.com Readers!';

<span style="COLOR: rgb(0,100,0)">  ?>

  编纂你的php.ini文件,找到”include_path”一行,把c:\public_html添加进去(分号分隔):

  1include_path = ".;C:\php\PEAR;C:\public_html"

  存盘退出,看下一步

  步调二:运转Web-Server

  切换到php的装置目次,敲下最要害的敕令—运转Web-server

  php -S 0.0.0.0:8080 -t C:\public_html router.php

  开端了吗?不要封闭窗口,假如历程封闭Web server也跟着封闭了。

  翻开阅读器:拜访http://localhost:8080/index.php吧,

  Hello cichui.com Readers!

  看到了吧?对,就是这个!

  提醒1:你可以思索自建一个php-server.bat的批处置,扔到桌面上今后就可以双击启动了。

  提醒2:运用0.0.0.0而不是localhost,可以包管外网不会拜访到你的web serve。

  精简的Array数组语法

  PHP 5.4为您送上精简的array数组语法:

<span style="COLOR: rgb(0,100,0)">  $fruits = array('apples', 'oranges', 'bananas'); // "old" way

<span style="COLOR: rgb(0,100,0)">  // 学Javascript的数组了

<span style="COLOR: rgb(0,100,0)">  $fruits = ['apples', 'oranges', 'bananas'];

<span style="COLOR: rgb(0,100,0)">  // 联系关系数组

<span style="COLOR: rgb(0,100,0)">  $array = [

<span style="COLOR: rgb(0,100,0)">  'foo' => 'bar',

<span style="COLOR: rgb(0,100,0)">  'bar' => 'foo'

<span style="COLOR: rgb(0,100,0)">  ];

  当然,旧语法照旧有用,我们多了一种选择。

  数构成员拜访解析(Array dereferencing*)

  处置数组再也不需求暂时变量了。

  假定我们需求获取Fang Bin Xin的middle name,

<span style="COLOR: rgb(0,100,0)">  echo explode(‘ ‘, ‘Fang Bin Xin’)[1]; // Bin

  PHP 5.4之前,我们需求如许:

<span style="COLOR: rgb(0,100,0)">  $tmp = explode(‘ ‘, ‘Fang Bin Xin’);

<span style="COLOR: rgb(0,100,0)">  echo $tmp[1]; // Bin

  目前,我们可以如许玩了:

<span style="COLOR: rgb(0,100,0)">  echo end(explode(‘ ‘, ‘Fang Bin Xin’)); // Xin

  再来个高级点的例子:

<span style="COLOR: rgb(0,100,0)">  function foobar()

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  return ['foo' => ['bar' => 'Hello']];

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  echo foobar()['foo']['bar']; // Hello

  *瓷锤注: Array dereferencing直译应为数组解除援用,结果欠安。其实更精确的翻译应为:“对函数返回后果的数构成员拜访解析支撑”,详见PHP官方分析。

  匿名函数中的$this

  目前,你可以在类实例中经过$this援用一个匿名函数(也叫闭包函数)

  <span style="COLOR: rgb(0,100,0)">class Foo

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  function hello() {

<span style="COLOR: rgb(0,100,0)">  echo 'Hello Cichui!';

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  function anonymous()

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  return function() {

<span style="COLOR: rgb(0,100,0)">  $this->hello(); // 之前是不成能这么玩的

<span style="COLOR: rgb(0,100,0)">  };

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  class Bar

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  function __construct(Foo $o) // object of class Foo typehint

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  $x = $o->anonymous(); // get Foo::hello()

<span style="COLOR: rgb(0,100,0)">  $x(); // execute Foo::hello()

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  new Bar(new Foo); // Hello Cichui!

  其实以前也能迁就用,就是有点费力: 

  <span style="COLOR: rgb(0,100,0)">function anonymous()

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  $that = $this; // $that is now $this

<span style="COLOR: rgb(0,100,0)">  return function() use ($that) {

<span style="COLOR: rgb(0,100,0)">  $that->hello();

<span style="COLOR: rgb(0,100,0)">  };

<span style="COLOR: rgb(0,100,0)">  }

  

  无论php.ini中若何装备,short_open_tag, 也就是 交换以前的了。

  支撑二进制直接量

  八进制(oct),前面加0;十六进制(hex),前面加0x;二进制(bin),目前在前面加0b就可以了

<span style="COLOR: rgb(0,100,0)">  echo 0b11111; // PHP 5.4支撑二进制了

<span style="COLOR: rgb(0,100,0)">  echo 31; // 十进制

<span style="COLOR: rgb(0,100,0)">  echo 0x1f; // 十六进制

<span style="COLOR: rgb(0,100,0)">  echo 037; // 八进制

  函数类型提醒

  自PHP 5.1起,类型提醒支撑对象和数组,PHP 5.4开端支撑callable。

<span style="COLOR: rgb(0,100,0)">  function my_function(callable $x)

<span style="COLOR: rgb(0,100,0)">  {

<span style="COLOR: rgb(0,100,0)">  return $x();

<span style="COLOR: rgb(0,100,0)">  }

<span style="COLOR: rgb(0,100,0)">  function my_callback_function(){return 'Hello Cichui!';}

<span style="COLOR: rgb(0,100,0)">  class Hello{static function hi(){return 'Hello Cichui!';}}

<span style="COLOR: rgb(0,100,0)">  class Hi{function hello(){return 'Hello Cichui!';}}

<span style="COLOR: rgb(0,100,0)">  echo my_function(function(){return 'Hello Cichui!';}); // 闭包函数

<span style="COLOR: rgb(0,100,0)">  echo my_function('my_callback_function'); // 回调函数

<span style="COLOR: rgb(0,100,0)">  echo my_function(['Hello', 'hi']); // 类名,静态办法

<span style="COLOR: rgb(0,100,0)">  echo my_function([(new Hi), 'hello']); // 类名,办法名

  高精度计时器

  此次引入了$_SERVER['REQUEST_TIME_FLOAT']数组变量,微秒级精度(百万分之一秒,float类型)。关于计算剧本运转工夫会十分有效:

<span style="COLOR: rgb(0,100,0)">  1echo 'Executed in ', round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 2)

发布:2007-03-31 14:47    编辑:泛普软件 · xiaona    [打印此页]    [关闭]
南昌OA系统
联系方式

成都公司:成都市成华区建设南路160号1层9号

重庆公司:重庆市江北区红旗河沟华创商务大厦18楼

咨询:400-8352-114

加微信,免费获取试用系统

QQ在线咨询

泛普南昌网站建设公司其他应用

南昌OA软件 南昌OA新闻动态 南昌OA信息化 南昌OA快博 南昌OA行业资讯 南昌软件开发公司 南昌门禁系统 南昌物业管理软件 南昌仓库管理软件 南昌餐饮管理软件 南昌网站建设公司