<?php
require_once dirname( __FILE__ ) . '/load.php';
// Non-programmers start reading here
$green_apple = My::$fruit->one_by_color_and_type( 'green', 'apple' );
echo '<h1>', $green_apple->color->name, ' apples are called "',
$green_apple->name, '".</h1>';
If you don’t know how to program, try reading this code. You can read it, right? That’s because the code is written using my newest project, FrontPress. Now, if you do know how to program, you may be wondering how My::$fruit is defined. Here’s the entire definition of My::$fruit:
<?php
class My_Fruit extends FP_Thing {
var $__table = 'fruit';
var $id;
var $name;
var $color;
var $type;
}
My::$fruit = new My_Fruit;
Now, you’re probably interested. The class has no functions and only one variable with a value. This is why FrontPress is so awesome. If you want to delete all the yellow fruit from your database, that’s one line of code:
My::$fruit->by_color( 'yellow' )->delete();
Or, maybe you just want to reclassify all yellow fruit as lemons:
My::$fruit->by_color( 'yellow' )->set( 'type', 'lemon' )->save();
You can also do so in three commands:
$yellow_fruit = My::$fruit->by_color( 'yellow' ); $yellow_fruit->type = 'lemon'; $yellow_fruit->save();
What if there’s some new color of apple you’ve never heard of?
$c0ffee = new My_Color( array( 'name' => 'C0FFEE', 'slug' => 'c0ffee', 'hex' => 'c0ffee' ) ); $c0ffee_apple = new My_Fruit( array( 'name' => 'C0FFEE Apple', 'color' => $c0ffee, 'type' => 'apple' ) ); $c0ffee_apple->save();
And it’s smart, too:
$lemon = My::$fruit->by_type( 'lemon' ); $lemon->save(); $lemon->save(); $lemon->save(); $lemon->save(); $lemon->save(); $lemon->save(); $lemon->save(); $lemon->save(); $lemon->save(); $lemon->save(); $lemon->save(); $lemon->save(); $lemon->save(); $lemon->save();
This code won’t even attempt to save it once because no changes are made to the object.
Want to try it? Break it? Change it? Go right ahead. This is GPL software, after all!
I don’t understand much of this post, but this is akchully a code I wrote in today. Successfully, of course. Full disclosure…with some help.