Posted: March 25th, 2010 | Author: gerrit | Filed under: PHP | No Comments »
Just a quick quick question: Why does php’s print_r function, which is pretty usefull to all, feature a $return (optional) argument? Not a question about it’s usefulness, but rather the choice to include this into the print_r. I would prefer to see an sprint_r variant behaving like
function sprint_r($sIn){
return print_r($sIn, true);
}
to conform to choices made for (s)printf. While on the subject, why not include a fprint_r as well (useful perhaps for logging):
function fprint_r($oFP, $sIn){
return fwrite($oFP, sprint_r($sIn));
}
and perhaps a vprint_r to complete the symmetry, though I have no clue as to what it should do
.
Posted: July 21st, 2009 | Author: gerrit | 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.
Posted: June 30th, 2009 | Author: gerrit | Filed under: PHP | No Comments »
Those 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 »
Posted: June 23rd, 2009 | Author: gerrit | Filed under: PHP | No Comments »
Often, 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 »
Posted: June 18th, 2009 | Author: gerrit | Filed under: PHP | No Comments »
Tiny 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 »
Posted: September 22nd, 2008 | Author: gerrit | Filed under: PHP | No Comments »
Enums are very usefull. Unfortunately php does not natively implement it as a type. Not to worry, there is an easy workaround. To implement a simple status enum having values for “ok”, “not-ok” and “uncertain”, try:
1
2
3
4
5
6
7
8
9
10
11
| abstract class Status implements Enum {
const UNCERTAIN = 0; // or any other value you wish
const OK = 1;
const NOT_OK = 2;
public static function values(){
return new ArrayIterator(array(Status::UNCERTAIN,
Status::OK,
Status::NOT_OK));
}
} |
The values of the enumeration are now immutable, which makes it preferable to implementing such a construct as an array. The enum interface provides the static values() function that allows the caller to iterate over the values that have been enumerated.
1
2
3
4
5
6
7
| interface Enum {
/**
* Return an array containing the enum values to iterate over
* @return Traversable Contains enum values
*/
public static function values();
} |
And that’s it. To use it, call Status::OK and the right value is used throughout your application. To loop, simply call foreach (Status::values() as $enum_value){...}.
This approach works well when several classes use the same naming for (some of) their properties. When using enums in a database, using this pattern will ensure the correct values are used. Also this will allow easy modification of the values in your app when the values in the database need to be changed.
Posted: September 19th, 2008 | Author: gerrit | Filed under: PHP | Tags: Performance | 2 Comments »
Representing data as XML has several advantages, all of which are not in the scope of this post. We will test several ways of creating a DOMDocument instance with a simple XML document. We will not be discussing differences between DOMDocument or SimpleXML or any of the other ways available to build XML structures. This post is concerned with the building (no manipulation either) of a simple XML tree.
Read the rest of this entry »
Posted: September 19th, 2008 | Author: gerrit | Filed under: PHP | Tags: Performance | No Comments »
Calling object methods can be done in several ways:
$oObject->method($param);
$sMethod = 'method'; $oObject->{$sMethod}($param);
call_user_func(array($oObject, 'method'), $param);
(call_user_method() function has been deprecated as of PHP 4.1.0, c.f. php.net.)
Intuitively the first type method calling is the preferred way to go both in terms of perfromance and readability. However, for the sake of generality (freedom, scripting or just exploiting the possibilities php provides), one of the latter two could be preferred. A quick performance comparisson may show which to use.
Read the rest of this entry »