在一行中声明多个属性

Declaration of multiple properties in one line

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

中声明一样

JQuery 例子

var a, b, c;

我可以期待里面 php class 像下面这样吗?

var $a, $b, $c;

我搜索了我的问题,但遗憾的是没有找到任何有用的信息。

是的,你可以

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

$i = new myclass();

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

使用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 中执行以下操作,例如:

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

}