PHP
  Home arrow PHP arrow Page 5 - Working with MySQL and Sessions to Serialize Objects in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Working with MySQL and Sessions to Serialize Objects in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 13
    2006-06-20


    Table of Contents:
  • Working with MySQL and Sessions to Serialize Objects in PHP
  • The basics of automated object serialization: using objects and sessions
  • Combining objects and sessions: defining a session handling class
  • Another implementation of object serialization: saving objects to MySQL tables
  • The complete list of classes for the example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Working with MySQL and Sessions to Serialize Objects in PHP - The complete list of classes for the example
    ( Page 5 of 5 )

    Finally, for the sake of completeness, below I listed all the classes that I used for constructing the previous example:

    // define 'User' class
    class User{
        var $name;
        var $address;
        var $email;
        function User($userData=array()){
            if(!is_array($userData)){
                trigger_error('User data must be an
    array!',E_USER_ERROR);
            }
            foreach($userData as $property=>$value){
                if($property==''||$value==''){
                    trigger_error('Invalid values for user
    data!',E_USER_ERROR);
                }
                $this->{$property}=$value;
            }
        }
        // obtain user name
        function getName(){
            return $this->name;
        }
        // obtain user address
        function getAddress(){
            return $this->address;
        }
        // obtain user email
        function getEmail(){
            return $this->email;
        }
    }
    // define 'ObjectSerializer' class
    class ObjectSerializer{
        function ObjectSerializer(){}
        function getSerializedObj($obj){
            if(!is_object($obj)){
                trigger_error($obj.'input parameter must be an
    object!',E_USER_ERROR);
            }
            return serialize($obj);
        }
        function getUnserializedObj($data){
            if(!is_string($data)){
                trigger_error($data. 'must be a
    string!',E_USER_ERROR);
            }
            return unserialize($data);
        }
    }
    // define 'MySQL' class
    class MySQL {
        var $conId; // connection identifier
        var $host; // MySQL host
        var $user; // MySQL username
        var $password; // MySQL password
        var $database; // MySQL database
        // constructor
        function MySQL($options=array()){
            // validate incoming parameters
            if(count($options)>0){
                foreach($options as $parameter=>$value){
                    if(empty($value)){
                        trigger_error('Invalid parameter
    '.$parameter,E_USER_ERROR);
                    }
                    $this->{$parameter}=$value;
                }
                // connect to MySQL
                $this->connectDB();
            }
            else {
                trigger_error('No connection parameters were
    provided',E_USER_ERROR);
            }
        }
        // connect to MYSQL server and select database
        function connectDB(){
            if(!$this->conId=mysql_connect($this->host,$this-
    >user,$this->password)){
                trigger_error('Error connecting to the
    server',E_USER_ERROR);
            }
            if(!mysql_select_db($this->database,$this->conId)){
                trigger_error('Error selecting
    database',E_USER_ERROR);
            }
        }
        // perform query
        function query($query){
            if(!$this->result=mysql_query($query,$this->conId)){
                trigger_error('Error performing query
    '.$query,E_USER_ERROR);
            }
            // return new Result object
            return new Result($this,$this->result); 
        }
    }
    // define 'Result' class
    class Result {
        var $mysql; // instance of MySQL object
        var $result; // result set
        function Result(&$mysql,$result){
            $this->mysql=&$mysql;
            $this->result=$result;
        }
        // fetch row
        function fetchRow(){
            return mysql_fetch_array($this->result,MYSQL_ASSOC);
        }
        // count rows
        function countRows(){
            if(!$rows=mysql_num_rows($this->result)){
                return false;
            }
            return $rows;
        }
        // count affected rows
        function countAffectedRows(){
            if(!$rows=mysql_affected_rows($this->mysql->conId)){
                trigger_error('Error counting affected
    rows',E_USER_ERROR);
            }
            return $rows;
        }
        // get ID from last inserted row
        function getInsertID(){
            if(!$id=mysql_insert_id($this->mysql->conId)){
                trigger_error('Error getting ID',E_USER_ERROR);
            }
            return $id;
        }
        // seek row
        function seekRow($row=0){
            if(!mysql_data_seek($this->result,$row)){
                trigger_error('Error seeking data',E_USER_ERROR);
            }
        }
        function getQueryResource(){
            return $this->result;
        }
    }

    To wrap up

    We’re done now. Over this series, I covered not only the fundamentals of object serialization in PHP, but also some advanced topics, such as the implementation of the magic “__sleep()” and “__wakeup()” functions, and the usage of objects in sessions. Finally, I completed the series by demonstrating how to store serialized objects in MySQL tables, which makes me believe that you have enough code to keep you experimenting for many hours. As usual, see you in the next PHP tutorial!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT