<?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
?>
|