Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As mentioned in comments, I'd implement a repository pattern for this case.</p> <p>For example, <code>User</code> would be a simple model with read-only properties</p> <pre><code>class User { private $phone, $status = 'default', $created, $modified; public function __construct($phone) { $this-&gt;setPhone($phone); $this-&gt;created = new DateTime; $this-&gt;modified = new DateTime; } private function setPhone($phone) { // validate phone here $this-&gt;phone = $phone; } public function getPhone() { return $this-&gt;phone; } public function getCreated() { return $this-&gt;created; } public function getModified() { return $this-&gt;modified; } } </code></pre> <p>Your repository interface could then look like this</p> <pre><code>interface UserRepository { /** * @return User */ public function findByPhone($phone); public function save(User $user); } </code></pre> <p>A concrete implementation of this interface could look something like this</p> <pre><code>class DbUserRepository implements UserRepository { private $pdo; public function __construct(PDO $pdo) { $this-&gt;pdo = $pdo; } public function findByPhone($phone) { // query db and get results, return null for not found, etc $user = new User($phone); // example setting the created date $reflectionClass = new ReflectionClass('User'); $reflectionProperty = $reflectionClass-&gt;getProperty('created'); $reflectionProperty-&gt;setAccessible(true); $created = new DateTime($res['created']); // create from DB value (simplified) $reflectionProperty-&gt;setValue($user, $created); return $user; } public function save(User $user) { // prepare statement and fetch values from model getters // execute statement, return result, throw errors as exceptions, etc } } </code></pre> <p>The cool thing here is that you can implement many different repositories, all with different persistence strategies (XML, test data, etc)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload