在一行中声明多个属性

时间:2015-07-15 09:24:27

标签: php oop properties

我可以在一行中声明多个php属性,特别是如果我不需要在那里初始化它们,就像我们可以在jquery中声明一样。

JQuery示例

var a, b, c;

我可以期待像下面的php类吗?

var $a, $b, $c;

我搜索了我的问题,但遗憾的是没有找到任何帮助。

2 个答案:

答案 0 :(得分:2)

是的,你可以

class myclass {
public $a, $b = 3, $c;
}

$i = new myclass();

$i->a = 2;
echo $i->a; // 2
echo $i->b; // 3

答案 1 :(得分:1)

使用var将不会在PHP 5.3中引发E_STRICT,这表示如果要创建此类声明,可以使用list

$info = array('coffee', 'brown', 'caffeine');
list($drink, $color, $power) = $info;
//you can use it like this
echo "I like to drink {$brown} {$coffee} with 100% {$caffeine}";

此外,您可以在类中执行以下操作,例如:

class Something {
   //this way
   private $one, $two;
   //also can be done with globals
   global $post, $product, $woocommerce;

}