This code will output the same. My question is, what is the correct way to do this. The first approach or the second? Or there is any better way? I don't see any advantage of one Class over the other.
<?php class Client{ var $id; var $email; function __construct($id){ $this->id=$id; } public function get_email($db){ $sql = $db -> prepare(" SELECT email FROM users WHERE id = ? "); $sql -> bind_param('i', $this->id); $sql->execute(); $sql->bind_result($email); if ($sql -> fetch()) { return $this->email=$email; } else return false; } } class Client_{ public function get_email($db, $id){ $sql = $db -> prepare(" SELECT email FROM users WHERE id = ?"); $sql -> bind_param('i', $id); $sql->execute(); $sql->bind_result($email); if ($sql -> fetch()) { return $email; } else return false; } } ?> index.php
<?php $Client = new Client(1); $a = $Client -> get_email($db); print_r($a); $Client_ = new Client_(); $b = $Client_ -> get_email($db, 1); print_r($b); ?>
$idas a string? It can just be an int, right?$dbin the contructor and then execute functionget_emailmethod every time you want some data.get_emailgets that user's email? Or does the class rather just model the database table and represents an interface to query that specific table, not tied to a specific user instance?