Ian Selby has posted a new tutorial today looking at something that can be very handy in the right situations - dynamically adding new functions to an already defined PHP class.
I've gotten a lot of great suggestions for features [for PHP Thumbnailer], and have wanted to add them, but at the same time don't as I would prefer not to bloat the class with all sorts of functionality. So I started thinking about how I could provide certain functionality for people that want it, without either simply making it a part of the class (and making it more bloated as a result), or coming up with all sorts of extended classes to maintain and distribute.
His solution was to add functionality dynamically to the class as plugins. Each plugin is defined as its own class (to keep things standardized) and will be included/executed by a base controller class. He includes some sample code showing how to create a basic user object that can store the first and last names of the user in question.
In this recent postJordi Boggiano looks at a different sort of design pattern - a sort of extension of the Singleton pattern: Multition.
While I like the Singleton pattern every now and then, I prefer the flexibility that the Multiton potentially offers, and well it's just an extended version of the Singleton, so it's "compatible" with the Singleton model. Anyway, to the point, PHP5.3 is coming, and with Late Static Binding you can do a base Multiton (or Singleton if you insist), which wasn't possible before. Now I like this very much because you can simply extend it rather than rewriting those (few, I know, but still) lines each time.
Included in the post is an example of the design pattern showing how to create its structure in the class and use it to grab the same or unique instances (defined with an ID).
In a new post to his blog, Brandon Savage makes a suggestion that could help in maintenance and debugging down the road - keep those superglobals out of your classes.
Let's ignore the security implications of the above code for just a moment, and focus on just the use of the superglobal. By using the $_POST superglobal array, we're effectively doing two things [in the example code]: relying on the field names and limiting code reuse.
He shows how to refactor the example into something a bit more reusable by changing the method call to pass in the given username and password instead of looking to the global for it. He does note, however, that there are some more correct uses for those superglobals:
There are some legitimate uses of superglobals in classes. One example is the use of the $_SESSION superglobal, which is often used for things like a user object. But I urge you to do so sparingly, when appropriate, rather than relying heavily on superglobals which are subject to change and may not give you the data you expect.
php|architect reminds you that today is the last day to take advantage of their iPod training promo offer:
There's still time to take advantage of our iPod Training Promo and get yourself up to 2 16GB iPods just in time for Christmas.
All you have to do to get in on the offer is sign up for one of their classes covering everything from an introduction to PHP, the Zend Certification and more advanced topics like PHP & Ajax development and advanced SQL and object oriented programming.
Knut Urdalen has passed along the announcement about the 1.0 release of the Yii framework for PHP being released (by Qiang Xue).
The Yii Framework builds upon learnings and findings from over 5 years of development of PRADO. If you already know PRADO you'll get the hold of Yii quite fast. The framework is already documented quite well in The Definitive Guide to Yii (a good place to start for beginners), in addition to complete class reference and an active forum where you can meet other developers.
There is also an extension repository and a few benchmarks to help you compare it to some of the other popular frameworks out there.
As an introduction to the Datetime class functionality PHP 5 has, Kevin Watersonshows how to use it to calculate the "Friday the 13th" for any year.
This class is a request from a PHPRO.ORG regular who asked for a method to calculate Friday the 13th for the next n years. The calculation itself is easy, however, it presents a good opportunity to introduce the PHP datetime class.
He points out that timestamps, because of they way integers are handled, have a somewhat limited lifespan (2038) and dates beyond that cannot be determined by the usual date functions. Instead, he shows how to set up a DateTime object, set a timezone and loop through the days to find the Fridays that land on the 13th.
The PHPro.org website has this new tutorial posted today - a step further into the would of OOP in PHP with a look at abstraction, hierarchies and polymorphism.
The PHP Object Oriented method of programming brings many exciting possibilities to application code. Many of the theories surrounding PHP Object Oriented code comes from some simple concepts. To the new comer, some of these concepts seem a little abstract, and with good reason. Abstraction is a key concept on Object Oriented code, but to the un-initiated, may seem rather vague.
Kevin looks at creating abstract classes as a foundation for types other classes can work from, overriding built in classes (like extending the DirectoryIterator to make a DirectoryReader class).
Jani Hartikainen has followed up on a post from David Otten about standard classes in PHP and how they provide the base for much of what the language does.
David Otton posted a short but thought-provoking post about stdClass, which many think is the "base class" all PHP classes automatically inherit from. I have to admit that I had this misconception as well. [...] This [difference in PHP from other OOP languages] presents some room for analysis in how things are handled in dynamic and static languages, and how those differences affect things...
Janitalks about dynamic and static typing in languages and how that effects the base types things are extended from as well as some of the benefits that having a standard base class affords developers.
David Otton has shared a discovery he's come across in his development - user-defined classes are not derived from stdClass.
Many OO languages have the concept of a single base class from which all other classes are explicitly or implicitly descended. For example, Ruby, Java and .NET all have Object. It's a very common belief that PHP implements stdClass as a base class for all objects, but this is in fact not the case.
He illustrates with a code example showing the results of calls to instanceof with a normal user class and one that extends the stdClass object.
This new post to Rudi's blog shows how, with the help of an external class you can use PHP to grab information from a torrent file.
To use this code you first need to include a file with the benc, bdec and hex2bin functions. To get the torrent data (like the announce url, the files etc) it is enough if we decode the content of the torrent. On the other hand, if we want to read the number of seeds, leechs and downloads, we must encode the info part of the torrent, encrypt it with the sha1 function and convert the result to lowercase.
He includes the code to make the decoding/encryption of the results a copy and paste away (once you have these functions to work from). He's even provided an example using the code that gets the information from a torrent on the Pirate Bay website.