Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14114

Models as Entities using PHP Traits

$
0
0
There are a couple of debates in some companies I've worked for about models being entities or not. In CI3, it usually ended something with 2 files: models/User_model.php and models/entities/User.php.

CI3 Example:
PHP Code:
$i $this->input;
$user = new User();
$user->email $i->post('email');
$user->password $i->post('password');
$this->user_model->create($user); 

But for future enhancements of CI4, PHP Traits can be used instead of extending \CodeIgniter\Model.
CI4 Example:
PHP Code:
final class UserModel
{
 private 
$email;
 private 
$pwd;

 use 
BasicCRUD;
 use 
AutoDBConnector;
 use 
InModelValidation;

 
//Email and password getters and setters...


This can be useful for something like:

PHP Code:
final class App
{
 private 
$users;
 public function 
addUser(UserModel $user):bool
 
{
 
//Add user from list.
 //Return bool.
 
}


The reason for this is for the developer to have the flexibility of whether or not his model is a real model, which can connect to a database, or just a plain entity by not using any model Traits at all.

Viewing all articles
Browse latest Browse all 14114

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>