note 95238 added to language.oop5.serialization

From: Date: Sun, 20 Dec 2009 20:39:21 +0000
Subject: note 95238 added to language.oop5.serialization
Groups: php.notes 
Request: Send a blank email to php-notes+get-164165@lists.php.net to get a copy of this message
PHP OBJECT SERIALIZATION I use a database to store info rather than storing PHP Objects themselves. However, I find that having a PHP Object acting as an interface to my db is way useful. For example, suppose I have a TABLE called 'user' that looks like this. CREATE TABLE user { user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, user_first VARCHAR(24) NOT NULL, user_last VARCHAR(24) NOT NULL, PRIMARY KEY (user_id) ); Then I would create a PHP Class definition like so: <?php require('includes/db_connect.php'); class User { protected $user_id; protected $user_first; protected $user_last; public function __construct($id, $first, $last) { $this->user_id = $id; $this->user_first = $first; $this->user_last = $last; } # FUNCTIONS TO RETRIEVE INFO - DESERIALIZE. public static function db_user_by_id($dbc, $id) { $query = "SELECT * FROM user WHERE user_id=$id LIMIT 1"; return User::db_select($dbc, $query); } public static function db_user_by_name($dbc, $first, $last) { $query = "SELECT * FROM user WHERE user_first='$first' AND user_last='$last' LIMIT 1"; return User::db_select($dbc, $query); } protected static function db_select($dbc, $query); { $result = mysqli_query($dbc, $query); if(mysqli_num_rows($result) > 0) { $row = mysqli_fetch_array($result, MYSQLI_NUM); return new User($row[0], $row[1], $row[2]); } } # FUNCTIONS TO SAVE INFO - SERIALIZE. public function insert($dbc) { $query = "INSERT INTO user VALUES (NULL, '$this->user_first', '$this->user_last')"; $result = mysqli_query($dbc, $query); } public function update($dbc) { $query = "UPDATE user SET user_first='$this->user_first', user_last='$this->user_last' WHERE user_id=$this->id LIMIT 1"; $result = mysqli_query($dbc, $query); } # GETTER and SETTER FUNCTIONS - DO NOT ALLOW SETTING OF ID public function getId() {return $this->user_id;) public function getFirst() {return $this->user_first;) public function getLast() {return $this->user_last;) public function setFirst($first) {$this->user_first = $first;} public function setLast($last) {$this->user_last = $last;} # CUSTOM FUNCTIONS public function getFullName() {return $this->user_first . ' ' . $this->user_last;} public function getLastFirst() {return $this->user_last . ', ' . $this->user_first;} } ?> Using PHP Objects for SERIALIZATION and DESERIALIZATION is now super-easy, for example: <?php require('User.php'); // INSERT a new user. $user = new User(0, 'Frank', 'American'); $user->insert($dbc); // done! // UPDATE an existing user. $user = User::db_user_by_id($dbc, 223); $user->setFirst('Johnny'); $user->update($dbc); // done! mysqli_close($dbc); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 69.85.144.213 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.serialization.php Edit -- https://master.php.net/note/edit/95238 Del: integrated -- https://master.php.net/note/delete/95238/integrated Del: useless -- https://master.php.net/note/delete/95238/useless Del: bad code -- https://master.php.net/note/delete/95238/bad+code Del: spam -- https://master.php.net/note/delete/95238/spam Del: non-english -- https://master.php.net/note/delete/95238/non-english Del: in docs -- https://master.php.net/note/delete/95238/in+docs Del: other reasons-- https://master.php.net/note/delete/95238 Reject -- https://master.php.net/note/reject/95238 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164165) next »