completely suppressing errors in php

This is something that's been bugging me for awhile, and I never really bothered to dig in and see what I could do to work around it, until now.

unserializing an unserializable string

The easiest, and most popular way to check if a string is serialized is simple.

PHP:
  1. /**
  2. * Is a string serialized?
  3. *
  4. * @param string
  5. * @return boolean  
  6. */
  7. function isSerialized($str)
  8. {
  9.     if ($str === 0)
  10.     {
  11.         return false;
  12.     }
  13.     else
  14.     {
  15.         return ($str == serialize(false) || @unserialize($str) !== false);
  16.     }
  17. }

Quite a few very popular frameworks and apps do it this way, try to just simply pass the string to the unserialize method, make sure it isn't the boolean false, and prepend the method with an @ to suppress notice-level error output. Don't get me wrong, it works. But if you're logging your notice-level errors, then any time you're trying to check if a string is serialized in this way, it logs the notice that's generated. Storing data as a serialized hash in a key-value database (Tokyo Cabinet — which we've chosen to use for Nomcat, BerkeleyDB, CouchDB, Redis, etc.) magnifies the scenario and takes this wee-little_old Notice, and makes your logs fill up really fast. Since error logs are for tracking issues that a developer should really concern themselves with, logging all of these notices just wouldn't do, it's simply a waste of space and processing time to handle them over and over and over again.

As we've been building Nomcat over at NOM, we're often faced with dozens of reads from the data storage layer with each view that's generated, and recently while testing, we've run into log files that look something like this.

CODE:
  1. ERROR - 2009-06-04 01:49:15 --> Severity: Notice  --> unserialize(): Error at offset 0 of 1 bytes /path/to/application/libraries/App_Model.php 432
  2. ... a couple hundred lines ...
  3. ERROR - 2009-06-04 01:51:19 --> Severity: Notice  --> unserialize(): Error at offset 0 of 1 bytes /path/to/application/libraries/App_Model.php 432

just set_error_handler to an empty method

Initially I tried a few different options to work around this annoying little problem.

  1. Prepending the unserialize method with @ to suppress output, but it doesn't prevent logging.
  2. Wrapping in a try-catch block doesn't do the trick
  3. Wrapping it with error_reporting(0) and error_reporting(2) doesn't do the trick either
  4. But creating an method to handle the error, have it to nothing, and wrapping the unserialize line with set_error_handler and restore_error_handler does the trick!

This is what I came up with.

PHP:
  1. /**
  2. * Is a string serialized?
  3. *
  4. * @param string
  5. * @return boolean  
  6. */
  7. function isSerialized($str)
  8. {
  9.     if ($str === 0)
  10.     {
  11.         return false;
  12.     }
  13.     else
  14.     {
  15.         set_error_handler(array('App_Model','unserialize_handler'));
  16.         $return = ($str == serialize(false) || @unserialize($str) !== false);
  17.         restore_error_handler();
  18.         return $return;
  19.     }
  20. }
  21.  
  22. /**
  23. * dummy error handler set up to obsorb any errors thrown by isSerialized
  24. * @return
  25. * @param object $errno
  26. * @param object $errstr
  27. */
  28. function unserialize_handler($errno, $errstr)
  29. {
  30.    // don't do anything
  31. }

If you noticed that I'm passing an array like this: set_error_handler(array('App_Model','unserialize_handler'));, you've got a good eye. This is how you can specify in which class to find the method you want to use as a callback, neither $this->methodname or Classname::methodname will work. However if you aren't writing object oriented code, you can simply pass the name of the method you want to use to handle the error as a string like this: set_error_handler('unserialize_handler');.

Let me know how it works for you.

11 rules to help you survive 2009

I had an entire post written about what this last year has meant for me, the good and the bad, what I'm grateful for, etc... However, I don't really write about anything personal here, I instead decided to take that post and send it as a personal email to a number of people who are close to me, and keep intimate communications intimate.

Instead, here's a few things I think might be good advice for surviving this upcoming year if you "work in tech". It's a living list, so if you feel that I've overlooked something that might touch you, let me know and I'll add to it.

  1. If you start a new company, don't make ad revenue it's core business model. If you're an adult, you should know better.
  2. Don't take a word and add "TW" to the beginning of it, it's not cute and instead you'll be an annoying and uncreative schmuck. This is an updated rule from last year's "if the word ends in 'ER', don't leave out the 'E'".
  3. Don't think for a second that people want to pay for content, they will however, pay for services. Now someone just needs to come up with better services.
  4. If you build something, make sure it's ACTUALLY useful, and not just cool and trite.
  5. If you work at an agency and enjoyed reading "Getting Real", please forget everything you read, those ideas work if you're on the product side, and not client services... that book is no good for you, get a refund.
  6. There's no such thing as a social media expert. People can be experts in platforms and tactics and skills, but not in "social media". I however am an expert is calling people out on misusing and abusing ambiguous terms and buzzwords. Like most buzzwords, it's something most people don't understand... when talking about it, I use the term 'unmanaged (brand) communications' because it actually describes what's happening; people are talking about all sorts of stuff, sometimes it might be about you, and it sometimes happens on the internet.
  7. SEO experts are a joke, you'll never need one if you have a good information architect, a copywriter and someone who knows how to write good, semantic markup.
  8. If you "follow" more than a few hundred people on 'you-know-what', you're a dick and need to cut that crap out right now. It's actually impossible to track more than 1000 people whether online or in the physical world. Quit fronting like you pay attention.
  9. If you're a strategist of some sort and don't have any actual or physical deliverables as part of your work, reevaluate how you actually are a benefit your clients or get a new career. I don't want to have to hunt you down and burn you at the stake, we don't need more people selling snake oil and practicing brand voodoo.
  10. No individual person is worth more than $150 per hour tops, if you disagree then you must be greedy, fleecing your clients, fleecing our industry, or all three.
  11. Don't waste your time getting excited about the new hot mobile device. They all suck, really. If you do need one, please recycle your old one and just buy the one that is the least painful for you to use.

Question of the day 11/5

I've decided to start posting general and non-technical questions that I have. This new feature of regular content will, obviously, be called "Question of the day"

It's one thing to manage, educate and cultivate relationships with existing clients; how important is it to educate a potential client, who you have already decided not to work with, as to why you aren't interested in working with them? (NOT why you aren't able, if you're simply unable)

Mashable’s open web awards are open!

Democratus needs your support

First let me thank Josh Klein for mentioning this on his blog before I got around to it...

So...

I have recently been working on starting a website called Democratus that will be a sort of "customer service platform for government" and provide a place for ideas for change to come directly from the people and be delivered directly to our representatives and press. We hope to be able to get this website off the ground by early next year, but in order to do that we're looking for some funding. Recently we applied for a grant from the Knight Foundation, who sponsor a contest for ideas like this, and we're very hopeful that this will provide the funding we need to launch the site, but we need your help to get the grant.

All you need to do is visit the website of the Knight Foundation and our page there, and register and vote on our idea. It should only take a minute or two and it would really help. If you want to read our application it is there on the website, and if you want to leave a comment on the proposal after reading it (something about how innovative and important this would be in your life, for instance) that would be even better.

Feel free to forward this post to other people that you think are interested in encouraging political participation and efforts to make our democracy more direct.

1) Register with the Knight Foundation: http://is.gd/6cTK

2) Vist our site: http://is.gd/6cU3

3) Vote and comment on our proposal (clicking on 5 stars won't hurt)

4) And then if you'd like, tell us what you think by sending us an email to Nate.Heasley@gmail.com or nater@naterkane.com

(Another) Scrollable Plugin for jQuery

I was having a bit of difficulty finding a jQuery plugin that not only provided scrolling behavior to a list of elements, but also used & then generated markup that could still be accessible, semantic & validate whether or not the user ran the javascript.

jQueryThe best plugin that I could find that was already out there was Tero Piirainen's jquery.scrollable.js. If you check it out you'll quickly see that the markup that's used to generate the scrolling items could be improved upon. Nothing in my version of jquery.scrollable.js removes functionality from what Tero put together, and the improvements are basic.

  1. make those items scrollable horizotally or vertically
  2. decide how many items are visible at once
  3. scroll elements with mouse, arrow keys and mousewheel (requires mousewheel.js)
  4. make navigational buttons without programming
  5. have programmatic actions: next, prev, nextPage, prevPage, seekTo, begin, end
  6. need to know when list is scrolled with custom event listener
  7. You can now start with semantic markup
  8. I want this all in single js file that weights only 3.9 4.1 Kb minified!

First we start with simple & semantic markup

There's nothing fancy going on here at all, just a wrapping element, an anchor and an unordered list. Unfortunately due to the design requirements of this particular implementation, an additional <div> had to be added. If it wasn't for the creative constraint, and the lack of support for vertical-align: middle across many browsers, this extra <div> would have not been included.

HTML:
  1. <div class="scrolling-list">
  2.     <a href="#newspage" class="more-link">More News</a>
  3.   <ol>
  4.         <li><div><a href="#">Someone reports second-quarter 2008 earnings &raquo;</a></div></li>
  5.         <li><div><a href="#">Bear scares comedian, everybody laughs &raquo;</a></div></li>
  6.         <li><div><a href="#">Someone Completes Someone else's Acquisition &raquo;</a></div></li>
  7.         <li><div><a href="#">Someone selected by Some Company, S.A.B. de C.V. for ADR program &raquo;</a></div></li>
  8.         <li><div><a href="#">Someone wins landmark multinational asset pooling mandate &raquo;</a></div></li>
  9.         <li><div><a href="#">Someone Announces Internal Restructuring Transactions and Guarantees Related to Acquisition &raquo;</a></div></li>
  10.         <li><div><a href="#">Someone wins comprehensive custodial mandate with one of New Zealand's largest organizations &raquo;</a></div></li>
  11.         <li><div><a href="#">Someone selected by East Timor to provide custodial services and training opportunities &raquo;</a></div></li>
  12.         <li><div><a href="#">Someone launches latest Global Derivatives Collateral offering &raquo;</a></div></li>
  13.         <li><div><a href="#">Someone expands Boston fund services operations &raquo;</a></div></li>
  14.   </ol>
  15. </div>

Presentation

Now we add styling that makes it match the look & feel of the main site.

The css required to make this bit work for you will be different for every project, so take a look at the included styles and tweak as necessary.

And now for the behavior

If the user's browser allows javascript to run, we add some additional unobtrusive behavior (including mousewheel support)

JavaScript:
  1. jQuery(function( $ ){
  2.     /**
  3.      * make sure the CSS knows that we're modifying the DOM and then style accordingly
  4.      **/       
  5.     $('.scrollable').addClass('js');
  6.     /**
  7.      * If you have an element you'd like to use the same style, but not attach the
  8.      * behavior you can turn it off by first adding the scrollable classname, but making
  9.      * sure the js classname is not applied to the element (this is what I did to show the
  10.      * "no javascript" version in the second example). Keeping the use of the scrollable
  11.      * classname for the general styles prevents having to write redundant styles
  12.      **/
  13.     $('.scrollable.nojs').removeClass('js');
  14.     /**
  15.      * a little DOM modification to keep all of the markup valid
  16.      **/
  17.     $('.scrollable.js ol').attr('id','items').wrap('<ul><li></li></ul>');
  18.     $('.scrollable.js ul>li').attr('id','itemswrapper');
  19.     var emptyLinkTarget = (window.opera) ? "#" : "javascript:function(){return}"; // a hash breaks a few flavors of IE & a javascript link (though it's poor form) breaks Opera.
  20.     $('.scrollable.js ul').prepend('<li><a class="prev" href="'+emptyLinkTarget+'">&lt;&lt;</a></li>').append('<li><a class="next" href="'+emptyLinkTarget+'">&gt;&gt;</a></li>');
  21.     /**
  22.      * and now we initiate
  23.      **/
  24.     $('.scrollable.js').scrollable({
  25.         size:3,
  26.         horizontal:false,
  27.         duration:1500,
  28.         items:'#items',
  29.         prev:'.prev',
  30.         next:'.next'
  31.     });
  32. });

This is the generated source we end up with... There's one thing you might notice, and that's the div class="__scrollable", though I initially placed that element outside of it's parent ol it did not work. It has something to do with how the xhtml 1.0 strict doctype is handled by the browsers. Though this may work in HTML 4.0 or various browser quirks modes, I haven't specifically tested all possible cases.

HTML:
  1. <div class="scrollable js">
  2.     <a href="#" class="more-link">More News</a>
  3.     <ul>
  4.         <li><a class="prev" href="javascript:function(){return}">&lt;&lt;</a></li>
  5.         <li id="itemswrapper">
  6.             <ol style="overflow: hidden; position: relative; visibility: visible; height: 124px;" id="items">
  7.                 <div class="__scrollable" style="position: absolute; height: 200000em;">
  8.                     <li><div><a href="#">Someone reports second-quarter 2008 earnings »</a></div></li>
  9.                     <li><div><a href="#">Bear scares comedian, everybody laughs »</a></div></li>
  10.                     <li><div><a href="#">Someone Completes Someone else's Acquisition »</a></div></li>
  11.                     <li><div><a href="#">Someone selected by Some Company, S.A.B. de C.V. for ADR program »</a></div></li>
  12.                     <li><div><a href="#">Someone wins landmark multinational asset pooling mandate »</a></div></li>
  13.                     <li><div><a href="#">Someone Announces Internal Restructuring Transactions and Guarantees Related to Acquisition »</a></div></li>
  14.                     <li><div><a href="#">Someone wins comprehensive custodial mandate with one of New Zealand's largest organizations »</a></div></li>
  15.                     <li><div><a href="#">Someone selected by East Timor to provide custodial services and training opportunities »</a></div></li>
  16.                     <li><div><a href="#">Someone launches latest Global Derivatives Collateral offering »</a></div></li>
  17.                     <li><div><a href="#">Someone expands Boston fund services operations »</a></div></li>
  18.                 </div>
  19.                 <br clear="all"/>
  20.             </ol>
  21.         </li>
  22.         <li><a class="next" href="javascript:function(){return}">&gt;&gt;</a></li>
  23.     </ul>
  24. </div>

This build is currently in a non-public svn repository, but I will be moving it to google code or github shortly (depending on whether or not I feel like learning how to use Git)

Standalone Demo:
http://www.naterkane.com/sandbox/scrollable/
Download:
First you'll of course need jQuery v.1.2.6
jquery.scrollable.js (7.5k)
jquery.scrollable.min.js (4.1k)
scrolling-list.css – the stylesheet used for the example
You can also grab a copy of jquery.mousewheel.js here


new NK&F logo

new NK&F logo
new NK&F logo, originally uploaded by naterkane.

it was time to update the very bogus & bad mark that we've been using for Nater Kane & Friends. comments are of course welcome.

verizon business dsl is less for my money

Image borrowed from http://hoboken411.com/archives/11797

Image borrowed from http://hoboken411.com/archives/11797

I'm so pissed right now, I pay a total of a couple hundred dollars a month across various ISP accounts, mobile phone accounts and television / broadcast service accounts... Due to where I live, I'm stuck with Verizon DSL, and I have no choice in the matter, so when their service is crap, I get especially frustrated. You can read a bit about what happened when I tried to get cable service installed here.

I thought I'd repost something that I just put up on everyone's favorite customer service site, Get Satisfaction.

"and the tech support i need anytime" is not available anytime

I have a dry-loop business DSL account. The telephone number associated with that dry loop account does not appear on any of my paper bills.

I call the verizon business tech support number, and am asked to either enter my number, or say "i don't have one" relying on their voice recognition.

if i call after 5pm, the telephone system instructs me to call back during normal business hours and then disconnects me. i understand sales being closed at 5pm, however tech support being closed is unacceptable.

my connection has been very spotty all day, and now i have to wait until monday to call to speak with their support staff to fix the problem? c'mon. this is supposed to be "business class" service (and support). I pay extra for it, expecting an extra level of support.

View the original post over here.

Verizon Business DSL support = FAIL

Basecamp phases out IE6

Basecamp phases out IE6
Basecamp phases out IE6, originally uploaded by naterkane.

This rules. Thanks for helping the web move forward guys!

Compatibility mode and Targeting IE with valid CSS selectors

I caught a link to Microsoft breaks IE8 interoperability promise an article written by Opera’s CTO, Hakon Lie.

Daniel Klima’s comment caught my eye, so I thought I might respond here in addition to writing a comment over here. At the time of this post, my comment hasn't yet been approved by moderators of theregister.co.uk

And it is needed as hell,since soem [sic] servers will send IE7 CSS to IE8,which causes IE8 to render page incorrectly.Daniel Klima

9 times out of 10 i have no need in my projects for IE conditional comments. Outside of the additional HTTP request loading an additional file brings, why add ANY browser specific code to a document? If you're using IE CSS filters, then sure, use conditional comments and don't break your validation, but unless you're forced in some way to use some old proprietary-to-IE scripting or styles, there's no point.

A star shines less brightly

That star (*) that we've relied to heavily upon to override ones own css with additional rules aimed at forcing weak and non-standards-compliant browsers to render content in a way that satisfies the ego's of creative directors everywhere isn't totally burnt out, but it's on it's way to becoming history.

I put together a little test to see what rules IE8b2 renders and ignores using selectors that won't invalidate you CSS. You can view the remarkably simple test here: ie8csstest

What this means

  • If you are currently targeting IE6 with the * html hack, it won't break your site if you run it in IE8.
  • If you are currently targeting IE7 with the *:first-child+html hack, it won't break a site in IE8 either.

Neither rule break css validation, IE8b2 in standards mode ignores both rules (but renders IE7's rule in compatibility mode), everyone is happy, and "servers don't send the wrong CSS to the browser".

So what's up with this broken looking button/icon?

Compatibility and Standards modes can be toggled with a small button that appears to the right of the location bar. If you are viewing a file locally using file:// the button is missing, so I have no idea what mode local files are rendered with. For local testing, a locally running server is required such as xampp or something similar.

Standards mode is default

Compatibility Mode Button - Off

Off

This is the compatibility modes button. Though it's not remarkably clear as to what it is, or what state it represents, when it is in it's "not selected" state, it means the browser rendering pages in Standards mode.

Compatibility mode

Compatibility Mode Button - Off

On

When the button appears to be depressed, the browser is rendering pages in Compatibility mode. Which due to my test's results, is the equivalent of IE7 mode.

Similar Posts

Categories

Archives

About

Nater Kane is freelance developer and user experience & technology consultant based in Brooklyn, NY.

Nater's focus is on creating a semantic and accessible web, and having delighted clients with happy customers.

He likes to spend time playing with his cats, playing drums, working on his diesel vw rabbit and his motorcycle, and enjoying a decent espresso.

RSS

Twitter » What I'm Up To

  • taking a 20 min break, then I guess I'll get back to work at 8. no rest for the weary! 31 mins ago
  • Sarah Palin is resigning? She'll no longer be the governor of Alaska? Good for her, better for Alaskans. #tcot 2 hrs ago
  • it's very much definitely lunch time. so what to have for lunch? @EatTime is making breakfast for me! woot! 5 hrs ago
  • More updates...

How I'm Social

Basecamp

Meta


Nater Kane naterkane personal http://www.naterkane.com LinkedIn Profile Web Technologist personal nater@naterkane.com 1978-09-12 voice 845.234.6698 | fax 707.922.0593
964 Flushing Ave. Brooklyn, NY 11206