Get started and transition to PHP7 (3)--uniform Variable syntax What's the unification?

Source: Internet
Author: User
Tags access properties parse error

PHP7 up and running

The original link of the park learning
Real-Park Learning video

Uniform Variable Syntax

Before PHP 7 proposed uniform Variable syntax, most of us probably didn't realize how "inconsistent" the previous version of PHP syntax was. And the fact is that in the variable-variable and variable-properties usage, PHP before the performance is indeed a bit confusing.

Now, PHP 7 fixes all of the previous syntax consistency issues and provides some new syntax, and we'll look at them.

Consistency Fixes

In a nutshell, PHP 7 takes the uniform scheme of "uniformly using left-to-right methods to evaluate expressions." Remembering this criterion is helpful for us to analyze some seemingly complex expressions. We understand the meaning of uniform by comparing the results of some code in PHP 7 and PHP 5. To be able to clearly indicate the order in which the same syntax was evaluated in different versions of PHP, we tagged it with {}.

<?php$ $var [' Key1 ' [' Key2 '];PHP 5.x${$var [' Key1 ' [' Key2 '};PHP 7{$ $var}[' Key1 ' [' Key2 '];ClassDemo {Public$prop;Public __construct () {$this->prop = [' Key ' =' Value '' Closure ' =function() { } ]; }}$var =New Demo;$var->prop[' Key '];//PHP 5.x $var->{prop[ ' key '] }; //PHP 7{ $var->prop}[ ' key ';  $var->prop[ ' key '] ();  $var->{prop[ ' key '} ();  $var->prop}[ ' key '] (); Classname:: $var [ ' key '] ();  PHP 5.xclassname::{ $var [ ' key '} ();  $var) [ ' key '] ();       
New syntax in PHP 7

In addition to the consistency of the existing syntax adjustment, Uniform Variable syntax also brought us a lot of new syntax.

Calling functions and Accessing properties

If the function returns an array containing closure, we can drop it like this:

<?phpfoo()[‘bar‘]();

If an object in the array has a specific property, we can access it in this way:

<?php[$obj1, $obj2][3]->prop;

If the function returns a string, we can use "{character subscript}" to access the characters in the string:

<?phpretString(){1};
Nested::

In addition to combining various expressions, we can also nest using::.

If Foo::bar () returns the name of a static class, we can access its static members in this way or call a static method:

<?php

Foo:bar()::$staticProp;Foo:bar()::$staticMethod();
nested function calls

As long as the expression returns a callable object, we can call them directly:

<?phpfoo()();Foo::bar()();$foo->bar()();
To dereference an arbitrary expression

Next, let's look at some of the more liberal expressions: We evaluate results based on expression expressions, which can be followed by various access properties and function calls:

<?php// (expression) is array(expression)[‘key‘]; // (expression) is a class object(expression)->foo();(expression)->prop;// (expression) is a class name(expression)::$foo;(expression)::foo();// (expression) is a string(expression){0};// (expression) is a callable object(expression)();

We can even dereference a scalar type, and if the string ' ClassName ' represents a class name, we can directly access the static members of the class:

<?php‘ClassName‘::staticMethod(); 

We can also invoke the static and member methods of the class in the form of an array:

<?php[‘ClassName‘, ‘staticMethod‘]();[$obj, ‘method‘]();
A Parse Error in PHP 7

In PHP 7, there is a syntax that is incompatible with PHP 5 and can result in parsing errors. When we define a global variable, PHP 7 does not allow the use of an expression that has ambiguity, such as:

<?phpglobal $$foo->bar; // Wrong in PHP 7

Will cause PHP 7 parsing errors, we must use {} to explicitly define the parsing order:

<?phpglobal ${$foo->bar};

 

Original:
1190000004154524



Get started and transition to PHP7 (3)--uniform Variable syntax What's the unification?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.