PHP:在类中创建这个类的新对象,对吗?
我可以在类中创建新对象吗 这是正确的标准php吗PHP:在类中创建这个类的新对象,对吗?,php,class,object,standards,Php,Class,Object,Standards,我可以在类中创建新对象吗 这是正确的标准php吗 <?php class cronjob{ private $k1, $m1; public function check_ready_to_moves(){ $move_arr = array( array( 'k1'=>'5', 'm1'=>'8', ), arra
<?php
class cronjob{
private $k1, $m1;
public function check_ready_to_moves(){
$move_arr = array(
array(
'k1'=>'5',
'm1'=>'8',
),
array(
'k1'=>'6',
'm1'=>'23',
),
); //this parameters are expamle. in fact read from DB
foreach($move_arr as $move){
$cron = new cronjob(); // Is this Correct??
$cron->set_package_param($move['k1'], $move['m1'])
->move_to_FTP();
}
}
public function set_package_param($k1, $m1){
$this->k1 = $k1;
$this->m1 = $m1;
return $this;
}
private function move_to_FTP(){
$this->k1;
$this->m1; //use these in function .....
}
private function integration_db(){
$this->k1;
$this->m1; //use these in function .....
}
}
?>
您有更好的代码建议吗?我认为您应该使用$this 像这样:
foreach($move_arr as $move){
// get rid of this line --- $cron = new cronjob(); // Is this Correct??
$this->set_package_param($move['k1'], $move['m1'])
->move_apk_to_FTP();
}
“正确”是什么意思?你试过了吗?它起作用了吗?有什么事情发生了吗?没关系。您还可以执行
newstatic()
,这样,如果需要,就不必在两个地方更新类名changes@onetrickpony:最好让它成为新的自我()。把LSB带到这里没有意义。
foreach($move_arr as $move){
// get rid of this line --- $cron = new cronjob(); // Is this Correct??
$this->set_package_param($move['k1'], $move['m1'])
->move_apk_to_FTP();
}