PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Indrek Altpere   ORM mapping class(like Hibernate), maps database table rows to objects   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: example file
Class: ORM mapping class(like Hibernate), maps database table rows to objects
Store and retrieve objects in MySQL table records
Author: By
Last change: Added missing require call to example script
Date: 16 years ago
Size: 1,285 bytes
 

Contents

Class file image Download
<?php
//you need to download the Mysql class into same directory or modify the require_once call to include it from other directory
//exampleobjects table is assumed to have a field 'name' and one record with id=1 in it at least
require_once 'Mysql.php';
require_once
'ExampleObject.php';
Mysql::Init('hostname', 'username', 'password', 'databasename');
$eo1 = ExampleObject::ById(1);//retrieves from database
$eo2 = ExampleObject::ById(1);//retrieves cached object from memory
echo $eo1->name.'<br>';
echo
$eo2->name.'<br>';//both same
$eo1->name = 'new name for me';//same objects will have new name value since both refer to same instance
echo $eo1->name.'<br>';
echo
$eo2->name.'<br>';//both same
$eo1->Save();//saves new name to database
$eo2->Save();//does nothing since the object was already saved and nothing has changed after last save
$eo3 = new ExampleObject();
$eo3->name = 'example 3';
$eo3id = $eo3->Create();
$eo4 = ExampleObject::ById($eo3id);
echo
$eo3->name.'<br>';
echo
$eo4->name.'<br>';//both same
$eo3->name = 'newname for example 3 object';
echo
$eo3->name.'<br>';
echo
$eo4->name.'<br>';//both same
$eo3->Save();//saves new name to database
$eo4->Save();//does nothing since same object was just saved and nothing has changed after
?>