使用 instanceof 检查和修改输入
Using instanceof to check and modify input
我正在使用 instanceof
检查 class 个实例,如果 class 已经有具有相同数据的实例,我想修改输入数据。下面的示例详细说明了我的问题,我有两个唯一的数组输入,第三个是第二个的重复数组,其中 instanceof
应该工作并修改输入。
/**
* Foo Class
*/
class Foo {
public $bar = array();
public function __construct() {}
public function add( $bar ) {
if ( $bar['ID'] instanceof Baz ) { // inctanceof not working as i am expecting. supposed to modify duplicate occurrence
//if bar['ID'] is already instance of Baz then we are trying to modify bar ID before pass it so Baz.
$bar['ID'] = $bar['ID'] . rand();
$this->bar[ $bar['ID'] ] = new Baz( $bar );
}
else {
$this->bar[ $bar['ID'] ] = new Baz( $bar );
}
}
}
Class巴兹
/**
* Class Baz
*/
class Baz {
public $ID;
public function __construct( $bar ) {
$this->ID = $bar['ID'];
}
}
实例
$foo = new Foo();
$bar = array( 'ID' => 'bar1' );
$foo->add( $bar );
$bar2 = array( 'ID' => 'bar2' );
$foo->add( $bar2 );
$bar3 = array( 'ID' => 'bar2' ); //duplicate ID
$foo->add( $bar3 );
打印
print_r( $foo );
输出
Foo Object
(
[bar] => Array
(
[bar1] => Baz Object
(
[ID] => bar1
)
[bar2] => Baz Object
(
[ID] => bar2
)
)
)
预期输出
Foo Object
(
[bar] => Array
(
[bar1] => Baz Object
(
[ID] => bar1
)
[bar2] => Baz Object
(
[ID] => bar2
)
[bar2{random number}] => Baz Object
(
[ID] => bar2{random number}
)
)
)
我在这里做错了什么?请指导我,替代解决方案也适用。
你 Foo class 应该看起来像:
class Foo {
public $bar = array();
public function __construct() {}
public function add( $bar ) {
if (isset($this->bar[ $bar['ID'] ]) && $this->bar[ $bar['ID'] ] instanceof Baz ) { // inctanceof not working as i am expecting. supposed to modify duplicate occurrence
//if bar['ID'] is already instance of Baz then we are trying to modify bar ID before pass it so Baz.
$bar['ID'] = $bar['ID'] . rand();
$this->bar[$bar['ID']]= new Baz( $bar );
}
else {
$this->bar[ $bar['ID'] ] = new Baz( $bar );
}
}
}
我正在使用 instanceof
检查 class 个实例,如果 class 已经有具有相同数据的实例,我想修改输入数据。下面的示例详细说明了我的问题,我有两个唯一的数组输入,第三个是第二个的重复数组,其中 instanceof
应该工作并修改输入。
/**
* Foo Class
*/
class Foo {
public $bar = array();
public function __construct() {}
public function add( $bar ) {
if ( $bar['ID'] instanceof Baz ) { // inctanceof not working as i am expecting. supposed to modify duplicate occurrence
//if bar['ID'] is already instance of Baz then we are trying to modify bar ID before pass it so Baz.
$bar['ID'] = $bar['ID'] . rand();
$this->bar[ $bar['ID'] ] = new Baz( $bar );
}
else {
$this->bar[ $bar['ID'] ] = new Baz( $bar );
}
}
}
Class巴兹
/**
* Class Baz
*/
class Baz {
public $ID;
public function __construct( $bar ) {
$this->ID = $bar['ID'];
}
}
实例
$foo = new Foo();
$bar = array( 'ID' => 'bar1' );
$foo->add( $bar );
$bar2 = array( 'ID' => 'bar2' );
$foo->add( $bar2 );
$bar3 = array( 'ID' => 'bar2' ); //duplicate ID
$foo->add( $bar3 );
打印
print_r( $foo );
输出
Foo Object
(
[bar] => Array
(
[bar1] => Baz Object
(
[ID] => bar1
)
[bar2] => Baz Object
(
[ID] => bar2
)
)
)
预期输出
Foo Object
(
[bar] => Array
(
[bar1] => Baz Object
(
[ID] => bar1
)
[bar2] => Baz Object
(
[ID] => bar2
)
[bar2{random number}] => Baz Object
(
[ID] => bar2{random number}
)
)
)
我在这里做错了什么?请指导我,替代解决方案也适用。
你 Foo class 应该看起来像:
class Foo {
public $bar = array();
public function __construct() {}
public function add( $bar ) {
if (isset($this->bar[ $bar['ID'] ]) && $this->bar[ $bar['ID'] ] instanceof Baz ) { // inctanceof not working as i am expecting. supposed to modify duplicate occurrence
//if bar['ID'] is already instance of Baz then we are trying to modify bar ID before pass it so Baz.
$bar['ID'] = $bar['ID'] . rand();
$this->bar[$bar['ID']]= new Baz( $bar );
}
else {
$this->bar[ $bar['ID'] ] = new Baz( $bar );
}
}
}