Cal Evans at Zend has posted my Solar Overview podcast. Thanks Cal! Click through to see the transcript (“script”, really ;-) of the audio.
In this episode, I’m going to give a brief overview of Solar project and how it helps with the mundane aspects of building applications.
Solar is an open-source library and framework for PHP 5; you can read more about it at solarphp.com.
Some early versions of Solar formed the basis of some parts of the Zend Framework, in particular the database and view components (this was around late 2005 and early 2006). Since that time, the two projects have continued to mature along separate paths, but the structure and organization of the two projects is still very similar, with one class per file using PEAR coding-style standards and E_STRICT compliance.
Solar originated from my attempts to build a PHP 4 framework out of PEAR components, a project I called Yawp. However, I encountered a number of difficulties in doing so. One of the biggest problems was, how to have each component automatically configure itself at construction time. It turns out that even though PEAR has a great set of coding-style standards, the different packages all have different construction and configuration. This meant that for each package I wanted to include in Yawp, I would have to build a wrapper specifically for it.
As a result, Solar uses a unified construction and configuration mechanism. Solar uses a single configuration file that returns PHP array, so there’s no parsing of ini, yaml, or xml files. The configuration values are all keyed on the related class name. All constructors have a single parameter, a config array, and the base constructor merges those values with the default values from the config file automatically. This means that if you write a class for Solar, it will configure itself automatically at the moment you instantiate it.
Additionally, and not to go on too long about configuration issues, but child classes inherit their parents’ default configuration values. This means that if you want a set of common configurations for a particular hierarchy, you can set them once in the config file, and all the child classes will use those — although you can override those in the child class, of course.
So that’s one thing that Solar automates for you, construction and configuration.
Something else Solar has is built-in localization. Each class that needs localized text has a subfolder called “Locale”, with a file for each country-and-language code. Like the config file, the locale file just returns a PHP array, with translation keys and string values. The Solar base class provides a method called locale() that automatically loads the right file for the class and returns singular or plural translations from that file. As with everything else in Solar, locale files are inherited along class hierarchies, so a child class uses its parent locale file by default, and can override any or all of the parent translations if it needs to.
The Solar base class comes with another method that helps in throwing exceptions. If you need to throw an exception in Solar, you call that method and give it a string error code. It then looks for the right exception class file for that code, gets a localized exception message from the locale file if one exists, and returns it for throwing. By now, you may have guessed that child classes inherit their parents’ exceptions, so you can start with very generic exception classes, and add specific ones as you need them, all without having to change your call for throwing the exception.
As you can tell, Solar makes a lot of use of class inheritance hierarchies as an organization and automation tool. This is only one example of Solar’s conceptual integrity, which is one of Solar’s greatest strengths. Anywhere you look in Solar, you will see things being done almost exactly the same way every time. We even go so far as to have standard names for methods. In some projects, the words “get” and “fetch” are interchangeable; in Solar, they have well-defined separate meanings. (As an aside, “fetch” means the method reads information from some external source and returns it, while “get” reads from a property or other internal value.)
Finally, Solar is built from the ground up with name-spacing in mind. Whle PHP doesn’t have real name-spaces, they are easy to emulate through a naming convention. Solar classes are fully name-spaced, and expects that developers will also name-space the code they build to work with Solar. Few if any PHP frameworks are fully name-spaced this way; some are name-spaced themselves, but do not allow for developers to extend into a different name-space if needed.
For example, a Zend Framework controller for “users” has to be called UserController; the moment you try to combine two separate projects that have a UserController, you will get name confliction issues. In Solar, you are expected to pick a top-level name-space for yourself and then extend into it; for example, you might have MyProject_App_Users. Because Solar already makes such extensive use of class hierarchies, it can tell how to address the controller automatically, even though it’s in a different name-space.
This has been just a brief overview of how the Solar Framework for PHP is organized; if you want to learn more, please visit solarphp.com.