PHP return model from model, why [closed] - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Tuesday, August 22, 2023

PHP return model from model, why [closed]

I'm totally new to PHP and Web Programming.

I just assigned to help in an old PHP Web project, the project use PHP version 5.6, with Yii Framework, I think it's version 2.

This is sample code from Controller

class sampleController extends AppController {
   public function actionSample($x) {
      // create model object here.
      $model = new sample();
      $data = $model->doSomething($x);
      
      // get model object from model object?
      $model = $data['model'];
      return $this->render('sampleview', [
         'model' => $model
      ]);
   }
}

This is sample code for model

class sample extends \yii\base\DynamicModel {
   public $somedata;

   public function doSomething($x) {
      // Some process here
      $somedata = $processresult;
      
      // create model object again
      $model = new sample();
      $model->somedata = $somedata;

      return ['model' => $model];
  }
}

Can any one explain, is it any reason to write the code like this?

I mean, since the model object already created in Controller, why need to create another model object again from the model object?

The code can work but why to write like this? From what I understand, since the model object already created in Controller, it looks like no point to create another model object again from the model object and return it to controller and pass to view.



source https://stackoverflow.com/questions/76947446/php-return-model-from-model-why

No comments:

Post a Comment