Object relational mapping is the process of creating a structural relationship between the classes/data objects used in the logic layer and the database itself.
One of the most successfully implemented exampled of an object relational/persistence frame work in the world of open source software is Seam with Hibernate. Seam is a framework for Java that allows one to run a generation script against a database and return an entire class frame work mirroring the structure of the database.
It allows for information to be directly loaded from the database into memory, view/altered/removed, and these changes saved without the user ever having to interact directly with the database through structured query language.
dbPHP is not designed to be commercial - if you want a hardcore resilient server with tons of functionality then you would never even consider PHP. what dbPHP aims to do is speed up the development times of quick and dirty PHP.
PHP allows users to develop code in incredibly shoddy ways with no regard for performance or re-usability and dbPHP tries to steer the developer towards a more structured object-oriented style of development.
Existing Frameworks
Zend Framework
WACT
Prado
ZooP Framework
eZ Components
CodeIgniter
CakePHP*
Symfony Project*
Seagull Framework*
PHP on TRAX*
My research has lead me to believe that only 4 of the existing main frameworks have object relational mapping capability *.
Object Relational Mapping
The importance of object relational mapping in scalable designs is clear. Maintaining a cross-platform relationship between data structures underpins many concepts which form the future of the internet - using an ORM Java example, a table can be taken from a database, loaded into memory and then directly output in XML - allowing for easy transfer of information retaining all of the semantic meaning using an open and widely available transport standard.
Lightweight.
The lightweight element of dbPHP is the key to its strength. PHP is free, it runs on cheap nasty hosting and it can provide a reasonable degree of functionality in relation the the manipulation and display of data for the web. dbPHP requires no installation other than being present in a directory. It is a series of classes that can be extended allowing child classes to map their internal structure to database objects and inheriting a number of useful SQL commands such as update, delete, findAll.
PHP Limitations
PHP has severe memory and performance limitations when it comes to handling large amounts of data or highly recursive tasks (default time out is set to 30 secs). It also has no ability to run threads and the only time it ever bothers to execute anything is if someone or something requests a page be served up. Global variables are seriously dodgy as you never really know what has access to them meaning that whenever you are requesting something from memory you have no way of knowing who set it, what's in it or if it will change.
PHP will quite happily work in a similar way to a well laid out java class model with private variables and get/set functions if one spends the time to do this.
Creative context.
When you want to make a project on the web, unless you love code, you don't really want to be spending half your time reinventing the wheel and rewriting code for boring banal stuff like update databases or other such stuff. Taking this to the extreme you can make all your projects in word press and totally abstract yourself from the process - if however your project involves some low-level elements, you're doing something clever with data, you want to incorporate a piece of flash that requires the storage of persisted data or you want to create a custom photo gallery of your own design - you probably want to focus on your own project and program logic. What dbphp does is allow you to treat your database and table as another area of memory and store and retrieve without extra hassle.
This returns the average colour of an image in PHP using GD libraries and then, assuming you ahve no sense of design, you could use this to set the background colour of a page. This code can be slotted directly into the ImageHandler class also on this site
This little application was an attempt to map an image into an array of pixel objects using the PHP GD libraries and it's made me painfully aware of how limited PHP is in it's ability to handle large or complex data structures.
Below is a simple piece of code that attempts to copy each pixel value from an image into a pixel object and store it in a 2 dimensional array - anything over 150x150px and you run out of memory/if you don't store it then it takes forever to process
The only way to implement this kind of functionality within PHP is to write a specific extension for that purpose.. but then that's not really PHP anymore and it means you have to mess around with your server configuration in order to make it work... oh for java on cheap boxes :(
Here is a little class I've written to make handling cookie sessions a little nicer in PHP. I'm not a big fan of session cookies but PHP makes a real mess of 'transparent' session ids with search engines - making it think you have infinite web pages.. so here goes
class CookieManager{
//Copyright Mike Lang - icurtain.co.uk - please retain this header and give credit if used.
//documentation
//set your cookie up or it will throw exceptions - name/value
//you can then create the cookie by calling $cookie->creatCookie();
//you can keep it alive with $cookie->keepActive();
//you can destroy it by calling $cookie->destroy(); which sets the expire date to the past
//plus you can do all the usual cookie config malarky - no point implementing a contructor that takes params
//as setcookie(); already does this.
//if you have any comments or suggestions for improving or changing this class feel free to mail me
//mike [at] bluemedia dot co dot uk
private $cookieName = null;
private $sessValue = null;
private $time = null;
private $expireTime = null;
private $url = null;
private $directory = null;
private $https = null;
private $httpOnly = null;
public function CookieManager(){
//note if your server is in a differnt time zone then the setTime function will break
$this->setTime(time());
$this->setExpireTime(0);
//default access to entire domain
$this->setDirectory('/');
//can be sent unencrypted by default
$this->setHttps(0);
//can only be sent over http
$this->setHttpOnly(1);
}
public function getCookieName(){
return $this->cookieName;
}
public function setCookieName($cookieName){
$this->cookieName = $cookieName;
}
public function getSessValue(){
return $this->sessValue;
}
public function setSessValue($sessValue){
$this->sessValue = $sessValue;
}
public function getExpireTime(){
return $this->expireTime;
}
public function setExpireTime($expireTime){
$this->expireTime = $expireTime;
}
public function setUrl($url){
$this->url = $url;
}
public function getUrl(){
return $this->url;
}
public function setDirectory($directory){
$this->directory = $directory;
}
public function getDirectory(){
return $this->directory;
}
public function setHttps($https){
$this->https = $https;
}
public function getHttps(){
return $this->https;
}
public function setHttpOnly($httpOnly){
$this->httpOnly = $httpOnly;
}
public function getHttpOnly(){
return $this->httpOnly;
}
//actual code
private function isValid(){
if($this->getSessValue()==null){
throw new exception('Session value not set');
}
if($this->getCookieName()==null){
throw new exception('Cookie name not set');
}
}
public function createCookie(){
$this->isValid();
$success = setcookie($this->cookieName,
$this->getSessValue(),
$this->getTime() + $this->getExpireTime(),
$this->getDirectory(),
$this->getUrl(),
$this->getHttps()
);
//only validates cookie creation - not user acceptance
return $success;
}
public function getCookie(){
if($this->isActive()){
return $_COOKIE[$this->cookieName];
}
return null;
}
public function isActive(){
return isset($_COOKIE[$this->cookieName]);
}
public function keepActive(){
if($this->isActive()){
$this->setTime(time());
$this->createCookie();
}
}
public function destroy(){
if($this->isActive()){
$this->setExpireTime(-10);
$this->createCookie();
}
}
//private functions
private function setTime($time){
$this->time = $time;
}
private function getTime(){
return $this->time;
}
}
//test harness
$blah = new CookieManager();
$blah -> setCookieName('bluemedia');
$blah -> setSessValue(rand());
$blah -> setExpireTime(200);
$blah -> createCookie();
if(!$blah->isActive()){
echo '
You probably don\'t have cookies enabled or this is the first time you have visited this page
PHP ArrayList Class - Having spent the last year working in Java, working with PHP now makes me cry and weep in anguish.. for many reasons, one of the main ones being its' complete inability to store more than 1 thing in anything other than an array
I have attempted to create a class that handles data in a marginally more friendly manner allowing you to interate through etc.. it's still a bit BETA but it speeds up my development times.. so here it is
class ArrayList {
//Copyright Mike Lang - icurtain.co.uk - please retain this header and give credit if used.
//This is a little class I've written to make my life easier and a little more like Java
//for those occasions when i just can't afford Java hosting
//It doesnt work the same as a Java arrayList.. it's just a high level aproximation of it
//if you have any comments or suggestions for improving or changing this class feel free to mail me
With it you can while($arrayList->hasNext()){} and do all kinds of other crazy things that will bring endless listing joy to your life - mail me if you have any improvement suggestions
This section of code validates a credit card or any other luhn number - useful as an initial card number check for merchant interfaces etc
the function returns either 0 or 1 depending on whether the luhn check was successful. The luhn check does a load of recursive calculation and then divides by 10 and checks if there is a remainder
function luhnCheck($number){
$sum = 0;
$alt = false;
for($i = strlen($number) - 1; $i >= 0; $i--){
$n = substr($number, $i, 1);
if($alt){
//square n
$n *= 2;
if($n > 9) {
//calculate remainder
$n = ($n % 10) +1;
}
}
$sum += $n;
$alt = !$alt;
}
//echo $sum;
//if $sum divides by 10 with no remainder then it's valid
instanceVar;
}
public static function staticFunction(){
return self::$staticVar;
}
}
$object = new TestClass();
//call transient on object
$object -> transientFunction();
//call function from class
TestClass::staticFunction();
?>