为什么我可以在php7中使用字符串文字作为类?

时间:2016-07-19 20:43:06

标签: php php-7

请考虑以下代码:

       <form *ngIf="showForm" [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)">
              <textarea  type="text"
                        placeholder="Description"
                        [formControl]="myForm.controls['message']">
              </textarea>
              <div class="msgError" *ngIf="myForm.controls['message'].touched">
               // Check for error in input and display error messages if invalid
              </div>

             <input type="text"
                       placeholder="Your mail"
                       [formControl]="myForm.controls['email']"/>
              <div class="emailError" *ngIf="myForm.controls['email'].touched">
              // Check for error in input and display error messages if invalid
              </div>
     </form>

它在PHP5中抛出一个错误(正如预期的那样):

  

解析错误:语法错误, [...] 4 中的意外'::'(T_PAAMAYIM_NEKUDOTAYIM)

但它在PHP7和输出中没有问题:

this.myForm = fb.group({
      'message' : ['',Validators.minLength(10)],
      'email' : ['',Validators.compose([Validators.required,this.emailValidator])],
    });
  }

这是故意还是错误?

3 个答案:

答案 0 :(得分:7)

我认为这是因为他们改写了有关评估的内容。

类似以下内容在PHP5中是不可能的,但在PHP 7中:

echo (new X)->toString();

同样适合

echo ('X')::$bar

请参阅Changes to the handling of indirect variables, properties, and methods

这主要是关于从左到右的评估,但它也影响评估。

可以在PHP RFC: Uniform Variable Syntax上找到更多信息(状态:已实施) - 感谢Nikic

  

此RFC建议引入内部一致性和   完整的变量语法。为了实现这个目标的一些语义   很少使用变量结构需要改变。

答案 1 :(得分:5)

PHP已经移动多年的总体方向是使用变量变量,变量函数和变量类更加灵活和通用。在PHP5中,当您想要使用变量类时,必须将类名放在变量中:

$class = 'foo';
echo $class::$foo;

看起来PHP7使这更通用,允许任何表达式,而不是需要变量。例如,您可以写:

$c1 = 'f';
$c2 = 'oo';
echo ($c1 . $c2)::$foo;

答案 2 :(得分:1)

我目前在文档中找不到任何内容,但我怀疑这可能是预期的行为。

在PHP 5中考虑以下内容,没有错误:

function hello()
{
    echo "hi";
}
$hi = 'hello';
$hi();

$hi是一个字符串,所以看起来他们只是决定添加对ad-hoc字符串的支持。

{7}在PHP7中有效是有道理的

<强>更新

您可以在{{3}}

的示例底部找到对PHP 7.0.0的一个小参考