Examples

PHP Namespaces and the XP Framework

at 2012-01-06 in RFCsPHP5Examples5.9-SERIES by friebe

With the implementation of RFC #0222, we have added optional PHP namespaces support to the XP Framework. Optional means the XP Framework itself will neither depend on PHP 5.3 (still supporting PHP 5.2.10 upward at least for the 5.9-SERIES) nor will it change any of its classes to use them. That doesn't mean you can't use them, though:-)

Here's a quick-start guide:

  • PHP namespaces use the backslash (\). These translate 1:1 to the package separator in the XP Framework, the dot (.).
  • Classes with PHP namespaces, fully-qualified and non-qualified XP classes may be mixed in one project. The XP group recommends migrating complete packages.
  • Inside classes using PHP namespaces, other XP classes need to be either addressed by their absolute fully-qualified names (e.g. \lang\Object) or imported via the use statement by their fully-qualified names; uses() may not be used there.
  • Inside classes not using PHP namespaces, namespaced classes must be added to the uses() list and addressed in their namespaced version.

As an example, if we have the following in de/thekid/tools/SQL.class.php:
  namespace de\thekid\tools;
use rdbms\DriverManager;

class SQL extends \lang\Object {
public
static function main(array $args) {
$conn= DriverManager::getConnection($args[0]);
// ...
}
}
To run this class, use xp de.thekid.tools.SQL ... as you would with a non-namespaced class.



Available database drivers

at 2011-01-16 in DatabasesExamples by friebe

Here's how to find out which database drivers are available (keys) - and what classes they're implemented by (values):

  Console::writeLine(DriverManager::getInstance()->drivers);
This will yield, depending on the PHP installation and configuration, something along the lines of:

  [
    mysql => lang.XPClass<rdbms.mysql.MySQLConnection>
    pgsql => lang.XPClass<rdbms.pgsql.PostgreSQLConnection>
    sqlite => lang.XPClass<rdbms.sqlite.SQLiteConnection>
  ]



ZIP files now support Unicode filenames

at 2011-01-16 in UnicodeExamples by friebe

The XP Framework's io.archive.zip package allows reading and creating ZIP files and is tested against zip archives created by Info-ZIP 3.0, PHP's Zip class, 7-zip, WinRAR and Windows' "compressed folders".

In SVN head, we have made a couple of adjustments to be able to support files written by Java's java.util.zip package (which is internally used by the jar command):

  • Reading the central directory to find the compressed and uncompressed lengths, Java doesn't write this into the local file header - making it impossible to stream those files
  • Supporting character set detection on extraction - up until JDK7 Build 57, the Java API was incorrectly writing entry names in UTF-8 but not setting the so-called "Language Encoding bit (EFS)".
  • Supporting to write archives with Unicode names - a JDK build downloaded yesterday still chokes on non-Unicode filenames inside ZIP archives when no charset is given (default is "CP437" here).
This way we can now create JAR files, for example.



The new lang.Closeable interface and ARM blocks

at 2010-12-31 in LanguageExamples5.8-SERIES by friebe

The new lang.Closeable interface and the io.streams classes being retrofitted to implement it may not seem very useful on the first glance, although they serve the purpose of supporting the so-called ARM blocks. These are a feature supported by XP Language and reuse the try keyword:

  try ($expression[, $expression[, ...]]) {
// Statements
}

Any of the expressions are expected to be instanceof lang.Closeable and their close() methods are guaranteed to be called in the declaration order regardless of whether the block raises an exception or not.


(more)

Hudson and the xml.meta API

at 2010-12-30 in Examples by friebe

Hudson LogoI'm currently working on a Hudson remoting API to be able to remotely configure our continuous integration projects. An example usecase occurred today: A co-worker had created a Hudson plugin which helps us distinguish projects with no tests at all from projects with tests exist: It display a different icon in the "weather" column and sets the build status to unstable. Now what we want to do is to enable this plugins in all projects, which would mean clicking through an odyssee of "Select Project - Configure - Enable - Save" more than 70 times in the frontend.

Hudson contains a Remote API which should be able to tackle this job.


(more)

apt-get meets the XP-Framework

at 2010-12-16 in ExamplesEditorial by friebe

Debian PackageThe XP Framework has an easy-to-use installation mechanism - simply downloading a setup script and piping it directly to PHP. What this mechanism cannot do though is to install PHP as a dependency itself, which is usually done in an operating-system dependant manner. On Debian and Ubuntu distributions, the packaging mechanism is called APT (Advanced Packaging Tool), which installs Debian packages (.deb files). In order to build such a package, we need to create a control file as well as the intended directory structure and wrap all that up using the "dpkg" tool (there's a howto over at IBM's developerworks). Unfortunately, this approach requires the Debian packaging tools to be available for the build platform - a situation we cannot rely on. This article shows a solution written in the XP Framework.


(more)

XP: Log SQL queries

at 2010-10-18 in ExamplesDatabases by friebe

Reading PHP: 62 characters to see all MySQL queries, I thought I'd post a similar use case solution here. The task is to log all SQL queries and a backtrace of where they're executed from. In Ulf's post, this is accomplished by an extension function written in C, which needs to be compiled and loaded into PHP.

Altough a nice feature, this way has several downsides, first of all, the compilation, a hurdle big enough to keep me from trying it out, and second, it's only going to work with MySQL databases (and to be precise, only those newer than 4.0, because mysqlnd doesn't support MySQL's old password protocol handshake).


(more)

First steps with the XP Language

at 2010-09-24 in LanguageExamples by friebe

The XP language is not completely different from the way PHP works, it keeps the flexibility and the general "look and feel" while adding syntactical support for features PHP doesn't support or doesn't support in all versions the XP Framework runs on.

Like in the XP framework, the entry point is always a class. In their most simple form, these classes have a static main() method. An example:

  public class HelloWorld {
public
static void main(string[] $args) {
util.cmd.Console::writeLine
('Hello World from ', self::class.getName(), '!');
}
}
OK, this doesn't look too unfamiliar, does it?


(more)

5.8 Feature showcase: Accessing class interna

at 2010-08-30 in Examples5.8-SERIES by friebe

NeoThe XP Framework's 5.8-SERIES contains a feature that will allow the reflection API to access private and protected members. Of course, just because you can, doesn't mean you should, as this does allow you to break the encapsulation principle.

On the other side, this feature comes in handy for serialization mechanisms, as well as for reflection-based factory or delegation patterns.


(more)

Generics of primitives

at 2010-08-10 in Examples5.8-SERIES by friebe

Like in C# (and unlike Java), primitives can be used as components of generics in the 5.8-SERIES. Given a vector util.collections.Vector<string>, the following applies:

  // OK
$v[]= "Hello";

// Throws a lang.IllegalArgumentException
$v[]= 1;

Primitives can also be used in other generic collections types, e.g. Set and Map implementations as well as Stack and Queue.



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases
5.8-SERIES
Unicode
Language
5.9-SERIES