note 129045 added to mysqli-result.fetch-object
| From: | joas at main dot php dot net | Date: | Wed, 15 Nov 2023 05:42:51 +0000 |
| Subject: | note 129045 added to mysqli-result.fetch-object | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221001@lists.php.net to get a copy of this message | ||
This function gets the object without calling the constructor or magic methods, quickly and easily.
The serialization of an array always starts with 'a', and the rest of the structure is the
same as for an object. We simply replace the 'a' with the header of the object:
<?php
class User {
public $id;
public $name;
}
function fetch_object($result, $class) {
$row = mysql_fetch_assoc($result);
if($row) {
return
unserialize('O:'.strlen($class).':"'.$class.'"'.substr(serialize($row),
1));
}
return null;
}
$object = fetch_object($result, User::class);
?>
I hope this help.
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 87.221.109.19)
----
Manual Page -- https://php.net/manual/en/mysqli-result.fetch-object.php
Edit -- https://main.php.net/note/edit/129045
Del: integrated -- https://main.php.net/note/delete/129045/integrated
Del: useless -- https://main.php.net/note/delete/129045/useless
Del: bad code -- https://main.php.net/note/delete/129045/bad+code
Del: spam -- https://main.php.net/note/delete/129045/spam
Del: non-english -- https://main.php.net/note/delete/129045/non-english
Del: in docs -- https://main.php.net/note/delete/129045/in+docs
Del: other reasons-- https://main.php.net/note/delete/129045
Reject -- https://main.php.net/note/reject/129045
Search -- https://main.php.net/manage/user-notes.php