__PHP_Incomplete_Class Object

Posted: July 21st, 2009 | Author: | Filed under: PHP | 3 Comments »

Just a quick note, as I could not find a satisfactory answer fast enough googling the web. I got a (not so) nice __PHP_Incomplete_Class Object notice the other day, happily programming until that moment. Such a notice indicates that somewhere (very likely in your session) a class has been unserialized that php does not know how to handle. This means that php knows which class the unserialized data should be, but cannot find the class declaration. It may happen using this type of code:

session_start();
class SomeClass { ... }
$_SESSION['some_instance']; // which is a SomeClass assigned in some previous request

Depending on the implementation of the class, e.g. use of SPL, this will result in a __PHP_Incomplete_Class Object. To fix this, tell php what the class is/can do before unserializing, i.e. move session_start below the class declaration.


MySQL Substr count

Posted: July 17th, 2009 | Author: | Filed under: MySQL | 1 Comment »

Recently I needed to create an SQL statement which was to count the occurences of a character inside a particular database field. For example, you could store the tags connected to a single db-record in a semi-colon (;) separated column in the record. To get the number of tags for this record straight from the database, we would thus like the number of semi-colons in the field (+1) as an indicator for this count. Since MySQL5 does not have a standard function for this (similar to PHP’s substr_count()), I had to find something of my own. Read the rest of this entry »


WordPress: Add more than one sidebar

Posted: July 10th, 2009 | Author: | Filed under: Wordpress | 1 Comment »

In wordpress it is possible to create multiple ‘sidebars’ with widgets on custom places in your template. To register a new sidebar open functions.php in your template folder (or create one), add:

if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'name' => 'SIDEBARNAME',
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '',
        'after_title' => '',
    ));

Explaination of the array() :

‘name’ = name of your new sidebar
‘before_widget’ = Add a html element before you start your widget (eg. <div class=”widget”)
‘after_widget’ = close the element you started above
‘before_title’ = Add a html element before your widget title (eg. <h3>)
‘after_title’ = close the element you started above

Now al you have to do is add your new sidebar to your place in your template, add:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('SIDEBARNAME') ) : ?>

This is what shows when no widgets are added.

<?php endif; ?>

WordPress: text-align fix

Posted: July 10th, 2009 | Author: | Filed under: CSS, Wordpress | Comments Off on WordPress: text-align fix

I just finished my new template and I was ready to test how it looks, great! but…oops a problem showed up and I couldn’t find what was wrong. Read the rest of this entry »


Sideskipping PHP’s Late Static Binding until 5.3

Posted: June 30th, 2009 | Author: | Filed under: PHP | Comments Off on Sideskipping PHP’s Late Static Binding until 5.3

bindingThose of you who have experience with mixing inheritance and static members have undoubtedly run into problems regarding late static binding. When some parent class higher up in the inheritance tree has static members (methods or attibutes), getting these to work with child-classes is impossible, e.g.:

class A {
    private static $sValue = 'show from class A';
    public static function show(){
        echo self::$sValue;
    }
}
class B extends A {
    private static $sValue = 'show from class B';
}
B::show(); // echos "show from class A", not "show from class B"

This behaviour is noted as “bug” and will be changed in version 5.3.
Read the rest of this entry »


MySQL and Integers

Posted: June 26th, 2009 | Author: | Filed under: MySQL | Tags: | Comments Off on MySQL and Integers

Also tired of always looking up the minimum and/or maximum value of a MySQL integer field? For once and for all a cheatsheet:

Field type Bytes Attribute Min. value Max. value
TINYINT 1 -128 (-2^7) 127 (2^7 – 1)
TINYINT 1 UNSIGNED 0 255 (2^8 – 1)
SMALLINT 2 -32768 (-2^15) 32767 (2^15 – 1)
SMALLINT 2 UNSIGNED 0 65535 (2^16 – 1)
MEDIUMINT 3 -8388608 (-2^23) 8388607 (2^23 – 1)
MEDIUMINT 3 UNSIGNED 0 16777215 (2^24 – 1)
INT 4 -2147483648 (-2^31) 2147483647 (2^31 – 1)
INT 4 UNSIGNED 0 4294967295 (2^32 – 1)
BIGINT 8 -9223372036854775808 (-2^63) 9223372036854775807 (2^63 – 1)
BIGINT 8 UNSIGNED 0 18446744073709551615 (2^64 – 1)

HTTP_REFERER in PHP

Posted: June 23rd, 2009 | Author: | Filed under: (X)HTML, JS, PHP | Tags: , , , | Comments Off on HTTP_REFERER in PHP

linkingThis morning I added some new features to a webapplication we are developing. They worked fine in Firefox, so I opened Internet Explorer to see if everything worked in there too. Of course it didn’t…

In webapplications you may need to know which webpage requested the current one. PHP offers an easy way to find the URL of that page: the predefined server variable $_SERVER[‘HTTP_REFERER’]. Unfortunately this variable was not set in IE. So where did it go?

Read the rest of this entry »


Searchresults with misspelled Names

Posted: June 23rd, 2009 | Author: | Filed under: PHP | Comments Off on Searchresults with misspelled Names

search resultsOften, we like to present our users with the best care possible. Unfortunately, when these users fill in the forms in applications, they tend to make type-o’s. This leaves us to correct them and “interpret” what our dear users meant. Obviously, in the interest of time (but more importantly: work), this task should be automated. As I sometimes think it is really difficult to interpret what other people mean, so may computers. This article is about correcting type-o’s in city names.

Read the rest of this entry »


DB-id’s to “readable” URLs (as in tinyurl, youtube)

Posted: June 18th, 2009 | Author: | Filed under: PHP | Comments Off on DB-id’s to “readable” URLs (as in tinyurl, youtube)

eyetestTiny urls come in handy in many different occations, such as twitter, chat messages and emails. For example, sharing this article with your friends could mean sending them the url http://nostylesheets.com/2009/06/17/db-ids-to-readable-urls-as-in-tinyurl-youtube, which is long. This length could be a problem, e.g. when the link is split over multiple lines in an email: the mail client may not recognize the continued link on the next line. A tiny url can be a solution to this: http://bit.ly/dirNA links to the same article and is much shorter. Several online services are available online, such as bit.ly or tinyurl.com.
Read the rest of this entry »


Getting propper UTF-8 output in PHP

Posted: May 29th, 2009 | Author: | Filed under: PHP | Comments Off on Getting propper UTF-8 output in PHP

utf8They are the nightmare of every PHP programmer: unknown characters suddenly showing up on your pages. The basic workaround is to htmlentity-encode everything. But one day you’ll get fed up with this dirty work-around and will want to get to the bottom of the problem. That’s what happend to me yesterday.

Read the rest of this entry »