PHP and Circular references

PHP, like most modern languages, has a garbage collection mechanism. It is supposed to destroy objects and variables as soon as there are no references to it anymore. To learn more about how it actually works, read this article. To summarize, PHP’s garbage handling is dumb, so any object that contains a circular reference will never be cleaned up. Normally, this doesn’t matter because a typical PHP request only takes a short amount of time and creates only a few objects. »

Brian Hartsock on #PHP,

PHP Classes and NULL characters

Today I ran into an interesting problem with PHP. It boils down to PHP’s handling of protected and private members of classes. Basically, when serializing or typecasting, NULL characters precede the variable names. For instance: class TestClass { private $var1; protected $var2; public $var3; } $instance = new TestClass(); //print_r() to a string and escape special characters $str1 = addslashes(print_r((array) $instance, true)); echo $str1; Outputs: Array ( [\\0TestClass\\0var1] => [\\0*\\0var2] => [var3] => ) As you can see, this is probably not what you expected. »

Brian Hartsock on #PHP,

PHP: explode() vs. split()

Even though I have been a PHP programmer for 4 years or so now, I still discover new things every day. While looking at different ways to split strings based on regular expressions I learned an important lesson. explode() isn’t the same as split()! Since I learned Perl before PHP, I prefer using split() and join() instead of explode() and implode(), respectively. To my surprise, split() is not an alias of explode() while join() is an alias of implode(). »

Brian Hartsock on #PHP,

More Active Record Features

It has been a while since I have written about, or coded, my Active Record implementation. Basically, I have had too many other things to work on to enhance the functionality of an already working, albeit very beta, design. But, that doesn’t mean I haven’t been thinking of all the features I want to add to it. For many applications, the date created and date modified information for data is very useful, but annoying to set. »

The buggiest PHP function

Well, its not really PHP’s fault. array_search is misused by developers more than any other PHP function I have seen. Just today, I found two bugs caused by misinformed developers using the function. I myself am also guilty of using this function incorrectly. The problem lies in array_search’s return value if the needle isn’t found in the haystack. If the needle isn’t found, array_search returns false. In my experience, many other libraries return -1 in this case. »

PHP Security class

So, you’re designing a new feature in PHP for an application. Like most good programmers, you have a data layer and a business logic layer. But where is your security layer? For the most part, whenever I design a new feature, all the security goes into the business logic, because the data layer should be “dumb”. Well, this is annoying because 1) I have a lot of repeat code and 2) it’s very easy to forget something. »

Brian Hartsock on #PHP,

A better web framework - Part 1

As I have mentioned before, design patterns are one of my favorite areas of computer science. Well, design patterns have a close association with web frameworks, so web frameworks are definitely another favorite area of mine. They both help speed up the development process by providing a structure for applications. Since Ruby on Rails took off a couple years ago, other web frameworks have sprung up in many other languages. None of these have met my needs at Webmail.us. »

PHP References revisited

After writing this post a few weeks ago about PHP references, I realized I missed one great use for references. The example scenario is this, you have an array of array’s representing rows returned from a database. You want to convert these arrays to specialized objects quickly and easily. $rows = $this->findRows(); foreach($rows as &$row){ $row = new RowObject($row); } Basically, what is happening is we are referencing the $row reference in the foreach loop. »

Brian Hartsock on #PHP,