Dre*_*gor 5 php dynamic variable-assignment assignment-operator
我正在尝试创建一个新对象,为其中一个成员分配一个值,然后将该对象分配给单个语句中的数组键.
IE:
class MyObj {
public $member = 'sad';
}
$myArray = array(
'key' => ((new MyObj())->member = 'happy')
);
Run Code Online (Sandbox Code Playgroud)
问题是这给了我一个语法错误:"语法错误,意外'='"
以下代码是一个解决方法:
$obj = new MyObj();
$obj->member = 'happy';
$myArray = array(
'key' => $obj
);
Run Code Online (Sandbox Code Playgroud)
问题是为什么单一陈述不起作用?为什么会产生语法错误?最后,如何创建一个创建新对象的语句,为其中一个成员变量赋值,然后将该对象与数组中的键相关联?
首先我想说,这是一个很好的问题。我不知道为什么它不起作用,但有一个简单的解决方法,即使用带有参数的构造函数来设置成员变量。事实上,构造函数是为了在初始化对象时触发代码而创建的,而这正是您正在尝试的。
class MyObj {
public $member;
public function __construct($x){
$this->member=$x;
}
}
$myArray = array(
'key' => new MyObj('happy')
);
print_r($myArray);
Run Code Online (Sandbox Code Playgroud)
编辑:我发现在 php 版本 5.4.0 之前,无法在初始化期间访问类。我使用的是 php 5.4.12,也无法使其工作,但它应该可以工作,因为它是自 5.4.0 版本以来添加的新功能。
http://docs.php.net/manual/en/migration54.new-features.php
添加了实例化时的类成员访问,例如 (new Foo)->bar()。
编辑:经过一些测试,我想出了这个实际有效的示例。当您尝试初始化一个新对象并同时访问其属性时,您就是按定义链接方法。在 php 版本 5.4.0 之前这是不可能的。如果您使用 php >= 5.4,则可以执行此操作。要初始化一个对象并同时访问其属性,您需要确保先前的方法调用返回该对象(new Obj 自动返回该对象)。PHP 不会自动执行此操作,因此您需要return $this手动执行。请参阅下一个编辑以获取解决方法。
class MyObj {
public $member;
public function setMember($x){
$this->member=$x;
return $this;
}
}
$myArray = array(
'key' => (new MyObj)->setMember('happy')
);
print_r($myArray);
Run Code Online (Sandbox Code Playgroud)
请注意,成员变量是通过返回对象的方法设置的。
编辑:(2014 年 11 月 2 日)
我创建了一个类,使子类中的方法可链接,而无需$this手动返回。它还可以启用和禁用方法链接,因此您可以同时执行这两种操作,而无需在不需要时返回 $this (请参阅start()和stop()方法)
Class Chainable{
private $chaining;//boolean
public function __call($method,$arguments) {
if(method_exists($this, $method)) {
//$res=call_user_func_array(array($this,$method),$arguments);
if($this->chaining ){
if(!isset(call_user_func_array(array($this,$method),$arguments))){
return $this;
}
else{
echo "you can't chain methods that return something";
//however you could add the returned values to an array
//(ie $this->returnedvalues)
}
}
}
}
private function setChaining($x){
$this->chaining=$x;
}
public function start(){
$this->setChaining(1);
return $this;
}
public function stop(){
$this->setChaining(0);
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用:可链接的方法必须是私有的或受保护的。公共方法不会被父__call()方法(Chainable 类)调用,因为它们超出了范围。
Class Foo extends Chainable{
public $par1;
public $par2;
protected function setPar1($x){
$this->par1=$x;
}
protected function setPar2($y){
$this->par2=$y;
}
public function getPar1(){//public functions are not visible to the __call method from parent class.
return $this->par1;
}
public function getPar2(){
return $this->par2;
}
protected function printPar1(){
echo $this->par1;
}
protected function printPar2(){
echo $this->par2;
}
}
$test=(new Foo())->start()//start chaining, set boolean true
->setPar1('hello')//chained method calls...
->setPar2('world')
->printPar1()
->printPar2()
->stop();//stop chaining
$test->setPar1('hi');//normal method calls...
$test->setPar2('you');
echo '<br>'.$test->getPar1().' '.$test->getPar2();
$test->start()->setPar1('bonjour')->setPar2('soleil');//start chaining again
$test->stop();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
202 次 |
| 最近记录: |