Showing posts with label oo. Show all posts
Showing posts with label oo. Show all posts

Tuesday, September 30, 2008

JAOO, day one

I am currently at the JAOO conference :-)

The conference seems to be buzzing with functional programming today. It started with the keynote by Anders Hejlsberg, where he talked about the future of programming languages, of which he claimed that one of the key components was functional programming. Concurrency was also mentioned as a key component. The talk also mentioned DSL's and used LINQ as example. It does seem nice - almost makes me want to play with C#. The F# language from Microsoft also seems like a nice alternative to C# if you wish the utilize the dot.net runtime. Very CAML/ML like.

A few other talks of the day made a great impression on me. I didn't see that many presentation, because I'm was working as crew most of the day.

I originally planned to go to the Aslaks rspec talk (I missed it a Rubyfools) but it was cancelled and replaced with a talk about meta-programming, and while it's an interesting topic, I've seen my share of those talks. Instead I decided to go the Fortress talk by Guy Steele - and it blew my mind! Fortress seems like a really interesting concurrent programming language. I will have to play around with this.

After the fortress talk I went to a talk about Scala, by Bill Venners. Scala is a functional language which runs on the JVM an integrates with java libraries. The talk it self was more of an introduction to functional programming for Java programmers and it didn't really mention any of the really interesting features Scala.

The final talk I went to was "Why Functional Programming (Still) Matters" by Erik Meijer. This was mindblowing. He talked about dealing with the "impurity" of languages introduced by side-effects by using monads. The idea of embracing side-effects - in the right way - rather than than shunning them was eye opening.

Sunday, October 9, 2005

Simulating polymorphism in PHP

Today, I was tearing hairs out of my head to find out how to do polymorphism as described by the Liskov substitution principle in PHP (version 4).

The problem I was faced with was the following: I have an object of supertype and I wish to use it as an object of a subtype. Apparently, this is not readily possible in PHP.

I found my answer in a comment to the php manual, credit to Simon Li. It's quite a hack.

What happens is that the object is serialized, it's transformed into a string like this:
string(32) "O:6:"answer":1:{s:6:"answer";i:42;}"
The class name, the third element, is replaced with the class name desired. And finally, the modified string is transformed into a real object again.

I generalized the code from the comment and ended up with following:

// hack to simulate polymorphism in php
// you're not gonna like it
class castable {
  function cast_to($name) {
    $tmp = explode(":",serialize($this));
    $tmp[1] = strlen($name);
    $tmp[2] = "\"$name\"";
    $this = unserialize(implode(":",$tmp));
  }
}

class answer extends castable {
  var $answer = 42;
}

class question extends answer {
  function tell_me_the_answer () {
    echo "The answer is: ";
  }
}

$deepthought = new answer();
$deepthought->cast_to("question");
$deepthought->tell_me_the_answer();

echo $deepthought->answer . "\n";


This outputs:

The answer is 42