PHP

Things I learned at the Oracle talk

It’s not complete notes from the session, but here’s a few interesting things I caught during Christopher Jones’ talk:

  • Oracle is introducing connection pooling for Oracle->PHP scripts (to be included with version 11g) – database resident connection pooling
  • you don’t need to redo the entire prepare/reparse of the oracle statement just because you change a value of a bind variable. You can just oci_execute and move on…
  • You get better performance if the table statistics are up to date
  • Check the oci8.statement_cache_size php.ini setting to tweak your fetching performance
  • Use oci_set_prefetch function (PHP5) to get a bit permance increase
  • ‘set autotrace on explain’ can give you more stats on your query and how it’s performing
  • Look into using TKPROF and STATSPACK (admin)

Looks like a bright future ahead for Oracle and PHP – especially with the new connection pooling. I can’t wait to give it a shot…

The T-Shirt Press Check Images

Well, the time has officially come and they’ve sent me images of the press check of the t-shirts! It’s always odd to see something you designed on an actual shirt 🙂 So, without further ado, here they are:

It looks like there might be a little bleed on the lettering on the back, but it still looks good. Oh, and no, that’s not me wearing the shirt 😉

A Single-function JSON Freakshow

Want to see something scary and hacky? Check this out – it takes in a PHP array and makes a JSON message out of it:

[php]
function build($arr,&$str,$lvl=0,$in_arr=”){
$lvl++;
foreach($arr as $key => $value){
if(is_array($value)){
$str.=(($in_arr==1)?'{‘:”).'”‘.$key.'”: ‘;;

if(count($value)>1){ $str.='[‘;
}elseif(count($value)==1){ $str.=”;
}else{ $str.='{‘; }

build($value,$str,$lvl,1);

if(count($value)>1){ $str.=’]’;
}elseif(count($value)==1){ $str.=”;
}else{ $str.=’}’; }

$str.=($in_arr==1) ? ‘}’ : ”;
$str.=(next($arr)) ? ‘,’ : ”;
}else{
$str.=($lvl>1) ? ‘{‘ : ”;
$str.='”‘.$key.'”:”‘.$value.'”‘;
$str.=($lvl>1) ? ‘}’ : ”;
$str.=(next($arr)) ? ‘,’ : ”;
}
}
}
[/php]

And, of course, a sample of how to use it:

[php]
$my_data=array(
“foo”=>”1”,
“bar”=>”2”,
“baz”=>array(
“one”=>”1”,
“two”=>”2”
),
“test”=>array(
“my_test”=>array(
“one_more”=>”1″,”three_more”=>”test”
),
“your_test”=>array(
“two_more”=>”2”
),
“nest1″=>array(
“nest2″=>array(
“nest”=>”test”
)
)
),
“meedle”=>”squeedle”
);

$str=”;
build($my_data,$str);
echo ‘{‘.$str.’}’;
[/php]

Be afraid. Be very afraid 🙂

Here’s to the unsung programmers….

A community is not about the members that get the most noteriety.

A community is not about the programmers that do the cool things or the bleeding edge stuff.

A community is not a select few.

A community is a group of people, centered around a cause, looking to do the best that they can to belong and to find thier place in the rankings that the community imposes.

See, the PHP community is like any other. There are those out there that are the notables, the ones that get mentioned in press releases or in lots of news postings (yes, guilty, I know). But for each of the ones that gets creadit for the advancements in the languge, there are tens or hundreds more behind them pushing and helpong to get things done.

Here’s to the unsung heroes of the PHP community – those developers that remain nameless, but contribute their 110% to the language or to developing their applications. They work thing fingers to the bone (well, okay, so maybe not quite that much) trying to further the cause of PHP. There are countless developers looking to forge their own path through the PHP wilderness without getting the recongition they deserve.

Yes, I’m guilty of focusing on a “chosen few” that have proven themselves in the past and have made a name for themselves in the community, but there are tons more out there, woorking on their own projects, making their own ways that aren’t acknowledged and that aren’t brought into the public spectrum.

So, here’s to you the unsung programmers out there – thank you for all that you do to help the PHP community become what it is.

If you’re at the ZendCon 2006 this year, find me and I’ll buy you a drink.

Baby Steps from ZF to Cake

So, I’ve been playing with CakePHP lately for a project I’m working up. I have to admit, it’s nice – very nice. I can see what all of the fuss is about.

Having used the Zend Framework for a project (a pretty important one, at least to me), I’ve had a bit of experience with the whole MVC style of development. I understaood what the parts did and how they fit together, but the actual development process was still a bit of a mystery to me until I really got into the ZF. I developed my application, worked up a pretty good site, and had things going along swimmingly.

Then I figured I’d give another framework a try for this other project. So, being the (brave?) soul that I am, I switched over to Cake. I have to admit, right off the bat, I almost ran screaming back to the Zend Framework. I was faced with a fully installed framework with a different structure than I was used to, different naming conventions and locations than I knew, and one big thing I wasn’t used to using coming from Zend – models.

So far, I’ve only used the models to act as interfaces to the database tables of their same name, but I know there’s more to them than that. Those working with the Zend Framework might not quite get how they’re used if you’ve never done any other MVC work outside of the Zend Framework, so here’s a bit of a basic summary as best I can tell:

Models are a “go between” for the controller and the view. The controller can look at and use the view to understand what the resource is like behind the scenes and use that to run code to output to the view. In the Zend Framework, since models aren’t even there, pulling information from and pushing data into database tables requires ensuring you have your SQL statements right to make the updates. In Cake, it’s as easy as calling the save() function in the controller and letting the framework (including the model for the table) so the rest.

Switching from one to the other was interesting – naming conventions and locations are less important to the Zend Framework as they are to Cake. Cake has a place for everything and everything in its place. There are directories under the views for the pages of each controller, helpers for each of the MVC components (components for controllers, helpers for views, and behaviors for models), and plenty of “magic methods” to go around.

I know I’m only getting started with Cake, but I do like what I’ve seen so far, and I know that there’s so many features of the framework that I’ve only touched on, but I’ll get there. I promise.

Oh, and, in a parting note, I have to compliment both frameworks on doing an excellent job on documenting their functionality. There might be a few little issues here and there, but over all, they both make life a lot easier through a few simple web pages.

I’ll Be There, Will You?

Well, it looks like I’m going to make it to some conferences this year after all (thanks to an understanding boss). They’re both right there next to each other, so it’ll be good to have it all right there.

First up is this fall’s The Ajax Experience put on by the guys at Ajaxian.com and the No Fluff Just Stuff Java Symposium Series. I’m not going to be speaking or anything, but since I write for the site, I’m going to be helping out with things. It’s running from October 23rd through the 25th up in Boston, Massachusetts, and if it was anything like the other one earlier this year, there’ll be some great speakers and great developers attending.

They’re still looking for speakers for the event, so if you’re up for giving an Ajax-related talk (preferably Ajax and PHP, but it doesn’t matter too much), check out this page for more details and the email address of where to send your talk information.

The next up, of course, is actually later that week – the Zend/PHP Conference and Expo happening out in San Jose, California. Thanks to the kind people organizing the conference, I’ll be one of the “press” attending the event. I’ve also taken out an ad in the conference program, so if you attend you’ll have to look for the PHPDeveloper.org 4th page ad. They haven’t posted the list of speakers yet, but I know it’ll be some good stuff – it always is. The tough part will be choosing which talks to attend.

I might also be doing some shameless self-promotion while I’m there. I was thinking about making up some shirts (something black with a simple logo-based design) to pass out to those that would want them. Think anyone would be interested? At the very least, it’d be fun to wear one myself during the conference. I’d stick out pretty well then, I guess…

Anyway, hope to see some of you out there at either of these conferences. I shouldn’t be too hard to find if you want to stop me and say “hi” while you’re there. I love meeting people that like PHPDeveloper.org….

php|tropics 2005 Pictures Flickred (yes, 2005)

So, I’m an (extreme) slacker and, spurred on by Cal posting his photos of this year’s OSCON, I proudly present to you something over a year too late:

My pictures from the php|tropics conference back in 2005.

Yes, call me a slacker, but I finally got them out there – few thought they may be. There’s only a few shots from the actual conference and more outside of it (go figure), but it was a great conference with tons of informative content.

Akismet is My Hero (including code!)

I’ll just start off with it – comment spam sucks.

Well, okay – really, any kind of spam sucks, but comment spam has been the bane of my existence lately. Initially, just to stop the incoming flow, I resorted to keyword matching. I grabbed strings from the current comments (around 6 thousand of them) and put them into a big array to help blog the evil little baddies hellbent on posting their worthless crap all over my comments (me? bitter? never!)

Sure it works, but it’s a pain to have to go back in each time and readd something when a new spam comes along. So, I told myself, “Self, surely there has to be a better way”. All hail the happiness that is Google – I found an answer within the first few pages of results, and that solution’s name is Akismet.

Now, I know what you’re thinking, “That’s just a WordPress plugin, right?” Wrong. They actually have a pretty slick (and simple! yay!) API to interface with their vast repository of spam-related things. All you have to do is provide them with the contents of the comment you’ve just recieved and they give you a “yay” or “nay” as to if it’s spam or not. The API takes in a traditional POST request and returns a normal response, in this case with either the string “true” or “false” (true meaning that it is spam).

Since it is a pretty easy interface to work with, I decided to throw together a little PHP class to make the connection and check to see if the message is spam (as well as check to see if your WordPress key is valid). Oh, did I mention that you need a WordPress key to get the setup working? You’ll need to go signup over on WordPress.com and, once logged in, go to your “My Dashboard” (top left link), go to the “Users” tab at the top, hit “Your Profile”, and right there at the top of the page you’ll see “Your WordPress.com API key is…”. Easy as pie.

With that in hand, you can grab the PHP class and go to town. What’s that? You want to know a little bit more about it? Well, okay…but just this once. And because I like you.

The class (made for PHP5, but is pretty simple to go back to PHP4 with) basically has seven functions:

  • Akismet() – the constructor, it just sets some class variables
  • _makeRequest() – actually does the work of sending the data off and getting the response back
  • _encodeRequest() – a simple function that loops and urlencodes an array
  • checkSpam() – generates the request to send off to the API to see if something is spam
  • checkKey() – calls a different server to see if the WordPress key you’re using is valid
  • submitSpam() – placeholder, for now.
  • submitHam() – placeholder for now.

Most of the class is pretty self-explanitory and is made to be used by just calling checkSpam() with the right parameters. As their documentation mentions, the more information you send along the better, but the simplest call I’ve seen that’s valid sends the content and author fields (comment_content and comment_author). The other fields that you can send are also listed in the documentation.

I know you’re hungry for some actual code by now, so we’ll get to the point. To make a simple checkSpam() call, here’s an example:
[php]
$arr=array(
‘comment_content’=>”viagra-test-123″,
‘comment_author’=>”enygma”
);
include_once(“/path/to/Akismet.php”);
$ak=new Akismet();
if($ak->checkSpam($arr)){
echo “this is spam and it sucks!”;
}else{
echo “we have an all-clear!”;
}
[/php]

Pretty easy, right? And any of the other fields you add into $arr (like permalink, comment_author_email, or comment_type) will be automatically included in the POST request out to the Akismet service.

It’s not the most robust system and there’s some other classes out there that are more well-developed than this one, but it’s light, easy, and it worked for what I needed it to do. Hopefully, it’ll help someone else out there too…

Oh, and incase you missed it – source: Akismet.phps