设为首页 收藏本站
查看: 574|回复: 0

[经验分享] What’s new in PHP 5.4? A huge list of major changes!

[复制链接]

尚未签到

发表于 2017-4-11 08:52:27 | 显示全部楼层 |阅读模式
    The PHP 5.4 is now available.
  As you probably know, the updates that were intended for postponed PHP 6 were added to PHP 5.4.0 instead, so now PHP includes a huge set of new language features and removes several legacy behaviors.
  Because of that I created a list of major changes since PHP 5.3 ,along with some examples and brief descriptions of these changes…

Major PHP improvements
  Changes since PHP 5.3 version include:



  • Added class member access on instantiation.  Now you can use fluent interfaces like in Java:

    $myCar = (new Car)->setSpeed(100)->setColor('blue');

  • Added callable typehint.  This typehint allows a string with a function name, a closure, and an array composed of classname (or object) with method name.

    <?php
    function foo(callable $cb) {
    $cb();
    }
    ?>

  • Added closure rebinding as parameter to bindTo.  Closure::bindTo() has been modified so now it accepts another argument that defines the new scope. This can either be an object if its class is used as the scope, or a class name.

    class A {
    private $x;
     
    public function __construct($v) {
    $this->x = $v;
    }
     
    public function getIncrementor() {
    return function() { return ++$this->x; };
    }
    }
     
    class B extends A {
    private $x;
     
    public function __construct($v) {
    parent::__construct($v);
    $this->x = $v*2;
    }
    }
     
    $a = new A(0);
    $b = new B(10);
     
    $ca = $a->getIncrementor();
    var_dump($ca());
     
    echo "Testing with scope given as object", "\n";
     
    $cb = $ca->bindTo($b, $b);
    $cb2 = Closure::bind($ca, $b, $b);
    var_dump($cb());
    var_dump($cb2());
     
    echo "Testing with scope as string", "\n";
     
    $cb = $ca->bindTo($b, 'B');
    $cb2 = Closure::bind($ca, $b, 'B');
    var_dump($cb());
    var_dump($cb2());
     
    $cb = $ca->bindTo($b, NULL);
    var_dump($cb());



      Result:

    int(1)
    Testing with scope given as object
    int(21)
    int(22)
    Testing with scope as string
    int(23)
    int(24)

  • Added short array syntax.  Makes PHP code more readable and maintainable.

    $a = [1, 2, 3];
    $b = ['foo' => 'orange', 'bar' => 'apple', 'baz' => 'lemon'];

  • Added binary value format.  Now it’s possible to use binary values directly in the PHP code:

    $x = 0b001110;
    echo $x;

  • Added support for Class::{expr}() syntax.  Makes PHP more flexible, when calling class/object methods.

    $method = 'method';
     
    $test = new Test();
     
    $test->method();
    $test->$method();
    $test->{'method'}();
     
    Test::method();
    Test::$method();
    Test::{'method'}();



      Result:

    method
    method
    method
    method
    method
    method

  • Added support for Traits.  A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

    class Base {
    public function sayHello() {
    echo 'Hello ';
    }
    }
     
    trait SayWorld {
    public function sayHello() {
    parent::sayHello();
    echo 'World!';
    }
    }
     
    class MyHelloWorld extends Base {
    use SayWorld;
    }
     
    $o = new MyHelloWorld();
    $o->sayHello();



      Result:

    Hello World!

  • Added closure $this support back.  Now you have an access to every object property (be it public or not).

    class A {
    private $value = 1;
     
    function firstGetter($name) {
    return function() use ($name) {
    return $this->$name;
    };
    }
     
    function secondGetter() {
    return function($name)  {
    return $this->$name;
    };
    }
    }
     
    $a = new A();
    $firstGetter = $a->firstGetter('value');
    echo $firstGetter();
    $secondGetter = $a->secondGetter();
    echo $secondGetter('value');



      Result:

    1
    1

  • Added array dereferencing support.  Provides the implementation of array dereferencing of method/function return.

    function fruit () {
    return array('a' => 'apple', 'b' => 'banana');
    }
     
    echo fruit()['a'];



      Result:

    apple

  • Added indirect method call through array.  Now $foo() also works in the cases where $foo is a callable array or Closure object.

    class Hello {
    static public function world($x) {
    echo "Hello, $x\n";
    }
    }
     
    function helloWorld($x) {
    echo "Hello, $x\n";
    }
     
    $callbacks = array(
    array('Hello', 'world'),
    function ($x) { echo "Hello, $x\n"; },
    'helloWorld'
    );
     
    foreach ($callbacks as $k => $callback) {
    if (is_callable($callback)) {
    $callback($k);
    }
    }



      Result:

    Hello, 0
    Hello, 1
    Hello, 2

  • Changed $GLOBALS into a JIT autoglobal.  $GLOBALS array is initialized only if it’s used. This is a performance/memory optimization, it can however break some of the existing scripts or opcode caches.


  • Improved performance of @ (silence) operator.  This can speed up PHP scripts which rely heavily on a silence operator, for example:

    $x = @file_get_contents('/etc/passwd');
    echo $x;

  • Added multibyte support by default.  Previously php had to be compiled with –enable-zend-multibyte. Now it can be enabled or disabled through zend.multibyte directive in php.ini.


  • Added built-in web server that is intended for testing purpose.  The following command will open a web server on the port 8000.

    php -S localhost:8000
  • Lots of performance and memory usage improvements

Removed major PHP features



  • Removed break/continue $var syntax.  You can no longer use variable to tell PHP how many levels of enclosing loops it should skip to the end of.


  • Removed safe mode and all related ini options.  Functionality described in this article and marked as depreciated in PHP 5.3 has now been removed


  • Removed register_globals and register_long_arrays ini options.  If enabled, register_globals injected PHP scripts with all sorts of variables, like request variables from HTML forms or values from GET requests. Now, every request/environment variable must be fetched from an appropriate PHP array.


  • Removed allow_call_time_pass_reference option.  Passing arguments by reference at function call time was deprecated for code-cleanliness reasons. A function can modify its arguments in an undocumented way if it didn’t declare that the argument shall be passed by reference. To prevent side-effects it’s better to specify which arguments are passed by reference in the function declaration only.

  For a full list of changes in PHP 5.4, see the ChangeLog. For source downloads please visit php.net QAT downloads page, Windows binaries can be found here.


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-363170-1-1.html 上篇帖子: PHP+ajax二级联动下拉选择菜单,IE+Firefox浏览器支持 下篇帖子: C++和PHP在面性对象特性上的对比
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表