Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're not going about this the right way. What you really want to do is have a class User that represents a single user, with methods reflecting this.</p> <p>From wikipedia:</p> <blockquote> <p>In object-oriented programming, a method is a subroutine that is exclusively associated either with a class (in which case it is called a class method or a static method) or with an object (in which case it is an instance method).</p> </blockquote> <p>A user object should at very least have instance methods enabling it to:</p> <ul> <li>Load from the database</li> <li>Save to the database</li> </ul> <p>And a static method to: - Create a user and return a user object.</p> <p>It should also have a constructor method (__construct(args) in PHP5 or User(args) in PHP4) to be called when the user is created. This should probably accept an id or a username or something identifying so it can load up the right user.</p> <p>For the sake of simplicity and not just doing everything for you, imagine a user object with just an id and a name. Here's how the class might look:</p> <p>Assuming PHP5:</p> <pre><code>class User{ private $id; public $name; public function __construct($id){ $this-&gt;load($id); } public function load($id){ // Do a query to load a user and initialize $id and $name. } public function save(){ // Do a query saving $this-&gt;id and $this-&gt;name to the database. } public static function create($name){ // Do a query to create a user with name $name. } } </code></pre> <p>You can load a user given his id using <code>new User($id)</code>, or create one given <code>User::create($name)</code></p> <p>At risk of being figuratively crucified, I wouldn't bother with setters and getters in PHP.</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