PHP:在类中创建这个类的新对象,是否正确?

时间:2013-12-17 13:07:15

标签: 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',
           ),
           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 .....

    }
}

?>

Insted of

        foreach($move_arr as $move){
            $cron = new cronjob(); // Is this Correct??
            $cron->set_package_param($move['k1'], $move['m1'])
                 ->move_to_FTP();
        }

你有更好的代码建议吗?

1 个答案:

答案 0 :(得分:1)

我认为你应该使用$ 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();
    }