English 中文(简体)
为什么你能够把操作者用在某类内部的阵列价值中?
原标题:Why can t you use operators in array values inside a class?
  • 时间:2012-05-11 21:56:07
  •  标签:
  • php
  • class

引证:

$test = array (2+2);
var_dump($test);

随后在课堂内进行同样的审判:

class test {
    public $test = array(2+2);
}

我只想知道,为什么有人会错过教区,也许会怎样解决(在课堂)这种容易使用守则和尽可能干的错误。

最佳回答

你们不能使用声明来初步确定分类领域。 它必须是字面的,具有不变的价值。 工作是使用建筑工:

class Test {
    public $test;

    public function __construct() {
        $this->test = array(2+2);
    }
}

From the manual:

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

问题回答

原因是,对某类财产的转让必须是静态申报。 不能说是评估的表述。

这是你可以做到的:

public $test = array(4); // static assignment
public $test =  some string ; // static assignment
public $test = strtoupper(   some string   ); // invalid, expression
public $test = $global_variable; // invalid, not a constant expression
public $test = time(); // invalid, an expression




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签