PHP Classes

Introspection !== reflection

Recommend this page to a friend!

      PHP Classes blog  >  Recommended PHP frame...  >  All threads  >  Introspection !== reflection  >  (Un) Subscribe thread alerts  
Subject:Introspection !== reflection
Summary:reflection isn't only about introspection
Author:Vaclav Sir
Date:2006-05-25 07:45:50
Update:2006-05-28 21:01:58

  1. Introspection !== reflection   Reply   Report abuse  
Picture of Vaclav Sir Vaclav Sir - 2006-05-25 17:02:30
I react to this:

<blockquote>...but it seemed he meant features like exception based error handling and object class introspection (AKA Reflection). If my guess is right, a shift from PHP to Ruby would not really have been necessary now. All those features are available in PHP 5.</blockquote>

Reflection API is a great innovation, but PHP still lacks good reflection support. Look at Javascript, for example:

// let's define a class
function MyClass() {
this.method1 = function() {}
this.method2 = function() {}

// modify already defined class (THIS is impossible in PHP)
// add a new method
MyClass.prototype.method3 = function() {}
// delete the first method
MyClass.prototype.method1 = null;

myObject = new MyClass();
// add a new method to the object
myObject.theNewMethod = function() {}

When features of the Runkit extension ( ) would be inbuilt in PHP, then I can agree that PHP has a "support for reflection". For now, it supports only "introspection".

  2. Re: Introspection !== reflection   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2006-05-25 18:00:54 - In reply to message 1 from Vaclav Sir
Right, but I believe most people do not need reflection for anything at all.

If you need to have code that works in a way that depends on something external to your software, like your database schema, I prefer resorting to code generation done before run-time.

Doing it at run-time is expensive. It takes more CPU, memory or disk space to achieve the same effect at run-time.

If you can anticipate all your code adaptation to a compile time phase, that will not impose run-time costs.

Since your database schema is not going to change at run-time, there is not much point doing code adaptation at run-time. When you want to upgrade your database schema to implement new features, just run the code generator again and install the new schema along with the new code version.

This is the way it works with Metastorage. This is code generation tool that generates Active-Record classes of objects according to your data model. The data model includes classes, variables, relationships and validation rules that are defined by you, the developer.

It also generates an updated database schema definition that can be upgraded easily using Metabase API at install time, so you do not loose previously recorded information in your database table records.

Metastorage was used to develop this blog and comment forum system. More and more things in the PHPClasses site will be built faster with Metastorage:

  3. Re: Introspection !== reflection   Reply   Report abuse  
Picture of Vaclav Sir Vaclav Sir - 2006-05-28 20:14:37 - In reply to message 2 from Manuel Lemos
I'm working on an ORM tool right now. I was using Propel and my #1 problem was the generation.

I wanted to write a wiki software with as little features as possible, extensible by plug-ins. But what to do, if a plug-in needs to extend the database schema? To require PHP-CLI for installation, or to distribute a server-side generator?

I think that generator is good solution for Java or ASP.NET. These platforms have the "deployment" between "writing" and "running". But it's not the PHP way.

My new ORM will be able to load the scheme on each request (it's fine when I'm developing) and of course, it will have got a caching mechanism. I am not going to leave PHP, I will use overloading. But reflection would be better...

  4. Re: Introspection !== reflection   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2006-05-28 21:01:58 - In reply to message 3 from Vaclav Sir
I don't know about Propel, but the ORM classes generated by Metastorage can be modularized and you can install the database schema parts that you need at run-time.

For instance, the PHPClasses site blog system uses the forum system to post comments about each blog post. The blog system could have a comment system or not if it was necessary, as the part of the each system database schema is installed in distinct steps.

All you need to do is to define each system using different Metastorage components. When the Metastorage generates the code for all the classes, it generates also a database schema part file in Metabase XML format. It also generates a schema installation class that calls Metabase API to install or update the schema as needed.

Therefore, to implement a plug-in based Wiki, you do not need to use reflection or whatever at run time.

The Metastorage approach is generates classes that are smaller and faster because they anticipate all optimization decisions to the generation time.

Generation time takes seconds but you do not need to carry it to your online application installation to be able to enable whatever plug-ins you want in the production environment.