Posts

Showing posts from April, 2008

Phomet changes name, so welcome Phico project

This is both an update and a news about my little Phomet project. I am sorry because I have not found time to write better examples or to explain possibilities as well, but I am sure you would like to know that now the project is called Phico (click there to download them) See you as soon as possible to talk again about Phico :geek:

Phomet - PHP Comet tiny library

Comet is a particular technique to interact asyncronously with the client. Instead of perform a lot of calls using Ajax (polling) the server is able to send to client, whenever it wants, a generic response. Unfortunately in the PHP world there's no simple way to implement this kind of technique and I'll write more posts to explain better how to implement this library and what is generally possible to do, or not possible yet, with Comet idea inside a dedicated PHP environment. At the moment, the only thing you can do is read documentation inside JavaScript and PHP files (truly few lines) and test the first basic example, a server side clock in less than 30 lines of mixed code. As last information, Phomet comes with all necessary to be optimized on client, and its final result is about 1.57Kb on client, and ridiculously 4.52 Kb on server, comments included :) Here is the download , unpack them into your localhost, and go into phomet folder to view the first demo. Compatibility? I...

Script Type PHP :)

Update 2008/03/15 I have removed preg_replace and added DOM classes to parse and manage, in a better way, script nodes that contain php code. Everything inside a simple PHP 5 compatible class . Finally, please remember that this is a layer between <?php ?> and JavaScript, and only an experiment ;) Here you can read another example, where difference between server, output, and client, should be more clear than precedent one. <?php require ' PHPScriptHandler.php '; ?> <html> <head> <title>Hello PHP World</title> <script type="text/php" author="andr3a"> // here we are between the server and output, known as client $hello = ucwords('hello php world'); // imagine that we would like to use a variable // defined somewhere in the server global $something; // we could even include or require php ...

Io programming language List for JavaScript

Io is a small, prototype-based programming language. The ideas in Io are mostly inspired by Smalltalk (all values are objects, all messages are dynamic), Self (prototype-based), NewtonScript (differential inheritance), Act1 (actors and futures for concurrency), LISP (code is a runtime inspectable/modifiable tree) and Lua (small, embeddable). This programming language is really interesting, starting from syntax, throw the entire guide . One of its primitive type is called List, and this is a summary of this type: A List is an array of references and supports all the standard array manipulation and enumeration methods. It seems that List is all we need when we think about an Array of elements ... so why couldn't we have something similar in JavaScript? // Io programming language List example // followed by my JavaScript List implementation a := List clone a = List.clone() a := list(33, "a") a = list(33, "a") a append("b") a.append("b") ==> ...

S.O.S. JavaScript - How to recover your stuff !!!

In this Ajax Web era there are a lot of sites that use JavaScript to perform simple or complex stuff. Sometime, one of these site could be "not so well" programmed, specially during client-server interactions. For example, it happens few days ago that while I was trying to post a message to another " friend ", an error occurred during this operation. The result was a beautiful fake popup with returned server error information and only a button to close them ... and I wondered what about my content? Can I try again to send them? The answer was NO , because the SEND button has been disabled, and the worst thing is that the textarea with my message was disabled as well. I wasn't able to get my message content again because of some client/server error and some bad logic in the client side. What could we do in these cases? Reading the source? ... uhm, content wasn't there ... Using firebug? Maybe, but content could not be there as well ... Press F5 and reload the...

Famous documentation and the dark side of "this" !

The good thing of internet is that you can find a lot of free documentation. At the same time, the bad thing of internet, is that this documentation is rarely updated. It could be a "guru documentation" or it could be a newbie documentation, but in both cases, it doesn't matter because it is probably wrong, not updated, or too generic. This post has not enough space to describe every error you can find on, and off line ... so let me start with some true example about a common misunderstood referer, the this one. Douglas Crockford and Private Members in JavaScript This page is famous enough, and I suppose that every true JavaScript developer has read them at least once. The main error in that page is described in this sentence By convention, we make a private that parameter. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functio...

Natural JavaScript private methods

I've never seen this technique yet, but basically it allows us to create private methods, without privileged , and in an way that does not allow subclasses to inherit them ... does it sound interesting? ;) As we can read in my JavaScript Prototypal Inheritance for Classical Emulation documentation, there is a way to easily create private methods without usage of privileged . The advantage of this way is, as explained in my doc, is that JavaScript interpreter does not have to create many functions for each declared instance . Here there is a basic example: // our constructor function Person(name, age){ this.name = name; this.age = age; }; // prototype assignment Person.prototype = (function(){ // we have a scope for private stuff // created once and not for every instance function toString(){ return this.name + " is " + this.age; }; // create the prototype and return them return { // never forget the constructor ... ...

PHP - JavaScript like Object class

As I've wrote in last post, there's some JavaScript feature I would like to have in PHP too. This time we will use a basic implementation of JavaScript Object constructor in PHP. What we need to start is this class, based on SPL ArrayAccess interface. class Object extends stdClass implements ArrayAccess { // (C) Andrea Giammarchi - webreflection.blogspot.com - Mit Style License // static public methods static public function create(){ return new Object; } static public function parseJSON($json){ return self::create()->extend(json_decode($json)); } static public function parseSource($source){ return self::create()->extend(unserialize($source)); } // basic JavaScript like methods public function extend(){ for($i = 0, $length = count($arguments = func_get_args()); $i foreach($arguments[$i] as $key => $value) $this->$key = $value; return $th...

PHP - apply, call, and Callback class

The "news" is that while some developer is putting a lot of effort to emulate PHP functionalities with JavaScript, I would like to have JavaScript functionalities in PHP. Nested functions, closures, and injected scope, are only some of JS cool stuff that's currently missing PHP (hoping that during this summer of code someone will implement at least one of them). Today what I'm going to emulate is a partial implementation of apply and call Function.prototype methods, and this is the basic example: function apply($name, array $arguments = array()){ return call_user_func_array($name, $arguments); } function call(){ $arguments = func_get_args(); return call_user_func_array(array_shift($arguments), $arguments); } As is for JavaScript, the main difference between these functions is that apply accept an array of arguments while call accepts an arbitrary number of arguments. echo call('md5', 'hello world'); // 5eb63bbbe01eeed093cb22bb8f5acdc3 ec...