note 114722 added to mysqli-result.fetch-object
| From: | me at philkershaw dot me | Date: | Fri, 28 Mar 2014 17:02:12 +0000 |
| Subject: | note 114722 added to mysqli-result.fetch-object | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199065@lists.php.net to get a copy of this message | ||
As a best practice, if you intend to use a defined class when using fetching_object(). Put the data
obtaining code within a static method of the defined class. Otherwise, wherever you include the file
(if not using an autoloader) the data connection will occur whether you want it to or not.
For example:
<?php
class User
{
public $name;
public static function getUser($id)
{
$conn = new mysqli('localhost', 'username', 'password',
'database');
if ($result = $conn->query("SELECT * FROM users WHERE id = {$id} LIMIT 1")) {
return $result->fetch_object('User');
$result->close();
}
}
}
?>
Call the static method to obtain an instance of the User class with your data applied to it.
<?php
$user = User::getUser('31');
echo $user->name; // echo's 'Phil'
?>
----
Server IP: 192.41.45.150
Probable Submitter: 82.68.82.229
----
Manual Page -- http://php.net/manual/en/mysqli-result.fetch-object.php
Edit -- https://master.php.net/note/edit/114722
Del: integrated -- https://master.php.net/note/delete/114722/integrated
Del: useless -- https://master.php.net/note/delete/114722/useless
Del: bad code -- https://master.php.net/note/delete/114722/bad+code
Del: spam -- https://master.php.net/note/delete/114722/spam
Del: non-english -- https://master.php.net/note/delete/114722/non-english
Del: in docs -- https://master.php.net/note/delete/114722/in+docs
Del: other reasons-- https://master.php.net/note/delete/114722
Reject -- https://master.php.net/note/reject/114722
Search -- https://master.php.net/manage/user-notes.php