Posts

Showing posts with the label PHP

A Better is_a Function for JS

In 2007 I have posted about get_class and is_a functions in JavaScript in order to simulate original PHP functions. Well ... that was crap, since a much simpler and meaningful version of the is_a function can be easily summarized like this: var is_a = function () { function verify(what) { // implicit objet representation // the way to test primitives too return this instanceof what; } return function is_a(who, what) { // only undefined and null // return always false return who == null ? false : verify.call(who, what) ; }; }(); ... or even smaller with explicit cast ... function is_a(who, what) { // only undefined and null // return always false return who == null ? false : Object(who) instanceof what ; } An " even smaller " alternative via @kentaromiura function is_a(who, what) { return who != null && Object(who) instanceof what; } Here a usage example: alert([ is_a(false, Boolean), // true is_...

JavaScript Hypertext Preprocessor

Image
well, the doctor said I can remove the tutor that was blocking my painful dislocated shoulder so here I am to test a "writing session" under pain killers from my bed! With such post title you may think that doctor was a shrink ... well, I let you figure it out after reading ;) About Hybrid Programming Languages We all know CoffeScript , right? As well as Traceur Compiler ... right? These are what I call Hybrid Programming Languages, or better, those kind of pseudo representation of the programming language we use, in this case still JavaScript, passing through a more abstract, enhanced, "improved" syntax (Coffee) or language features (Traceur) . About PHP And JavaScript If you follow me since years you may have noticed how many times I have tried to make PHP more JavaScriptish . JavaScript like Object class is the first example from 2008, while [js.php] A JavaScript Object Like PHP Class was surely a better attempt, thanks to new PHP 5.3 features, as well as PHP...

PHP 5.3.5 Update on PAMPA-J

Quick post, for one of those 7000 developers that use PAMPA , there is a new zip upgrade to download able to bring PHP 5.3.5 in your PAMPA folder. The structure inside this zip is the same one, the service should be stopped or quitted before you can extract and replace all files. Please backup your current php.ini file, inside PAMPA/config folder, in order to eventually update the new version or simply put it back as it was before ( really few changes there since the 5.3.0RC1 ). The destiny of this project is still under evaluation but at least now it is offered with the latest, stable, PHP version. Have fun ;)

WebSocket Handshake 76 Simplified

update there was a superfluous CR+LN with char 0x00 that was causing buffer troubles, now fixed I am working during my free time (... recently extremely hard to have ...) over a little project that I'd like to show at the Front Trends event this October and WebSocket is the key of this project. While 2 days ago I eventually found a way to communicate in few lines of php with a WebSocket, yesterday Chromium blog announced they " simply changed it ", causing basically problems to all those projects based over the good old handshake75. After I have found in Axod's Hack that somebody else had basically my same thoughts, I still could not find any valid example able to do the new handshake ... so here I am with the first draft-ietf-hybi-thewebsocketprotocol-00 php implementation I know, inspired somehow from the go version . <?php class WebSocketHandshake { /*! Easy way to handshake a WebSocket via draft-ietf-hybi-thewebsocketprotocol-00 * @link http://w...

JSON __sleep, __wakeup, serialize and unserialize

The JSON protocol is a de facto standard used in many different environments to transport objects, included arrays and Dates plus primitives such: strings, numbers, and booleans ... so far, so good! Since this protocol is widely adopted but it has not the power that a well known function as is the PHP serialize one has, we are often forced to remember how data has been stored, what does this data represent, and eventually convert data back if we are dealing with instances rather than native hashes. If we consider the ExtJS library architecture, we can basically have a snapshot of whatever component simply storing its configuration object. The type will eventually tell us how to retrieve a copy of the original component back and without effort at all. Since JSON is the preferred protocol to store data in WebStorages, databases, files, etc etc, and since we can save basically only hashes, loosing every other property, I have decided to try this experiment able to bring some magic in the...

PHP Serialization And Recursion Demystified

Introduction PHP has different in-core callbacks able to help us with daily deployment, debug, improvements. At the same time, PHP is loads of intrinsic " gotcha ", too often hard to understand, hard to explain, or simply hard to manage. One common problem is about debug, caching, or freezing, and the way we would like to debug, cache, or freeze, variables. For freezing, I mean those procedures able to regenerate a stored variable and its status, in order to reuse that variable, to understand what happened in that moment with that variable, or just to speed up expensive tasks already completed. The Problem One of the most common procedures to freeze variables is their serialization, performed in core via a well known serialize function. Please consider this example: $person = new Employee('Mr. Lucky Me'); // ... do some useful task myCompanyFreezer($person); // the myCompanyFreezer function function myCompanyFreezer(Employee $p){ $company = Company::getInstance()...

PHP How to var_export a debug_backtrace

Just a quick post, and I am not sure if this is documented, or if this works with every PHP version, but apparently a truly common problem with var_export is the logical inability to manage recursions. Well, one of the most important function in PHP is the debug_backtrace , which contain recursion, and one thing you would probably like to know is that this trick allowed me to avoid recursion problems: $backtrace = unserialize( serialize(debug_backtrace()) ); After this, it is possible to use json_encode, var_export, or whatever other serializer. The reason is that serialization discover and remove some implicit recursion, so that unserialize will return a cleaner object. This technique has been used in Formaldehyde , but manual recursions are not supported yet (for example via formaldehyde_log). recursions, for a debug purpose, are in any case redundant. All we need to know is what is there, rather than where. Regards

Formaldehyde - Ajax PHP Error Debugger

Image
I am proud to announce my last Web 2.Next creation, Formaldehyde , the most simple, lightweight, scalable, and complete (for its simplicity) Ajax and PHP Error Debugger. I described everything in its dedicated Google Code Project Page , but just to summarize without many other words, this is the common deployment situation : while this is what's up with a single formaldehyde.php file inclusion: I hope you will like it, can't wait for some comment :geek:

PHP 5.3 Singleton - Fast And Abstract

In this same blog I talked different times about Singleton Pattern , in latter link " poorly " implemented in PHP 5. I say poorly, because being Singleton a pattern, there are many ways to implement it and via PHP 5.3 things are more interesting. There are several ways to define a Singleton class and to extend it, being able to automatically create another one that will follow that pattern. Here is my implementation, which is extremely fast, logic, and simple as well. <?php // Singleton :: The WebReflection Way namespace pattern; // this is just a pattern ... // so no new Singleton is allowed // thanks ot abstract definition abstract class Singleton { // note, no static $INSTANCE declaration // this makes next declaration a must have // for any extended class // protected static $INSTANCE; // @constructor final private function __construct() { // if called twice .... if(isset(static::$INSTANCE)) // throws an Exception ...

PAMPA-J Destiny?

One of my favorite Windows Desktop Application Project, PAMPA-J , is not dead ... it is simply " waiting events ", if any. What Is Going On With Aptana Jaxer I have no idea! The most exciting news about PAMPA was the J at the end: Jaxer . Apparently this project is in stand-by: while it has been always present in the Aptana Home Page , it disappeared even from Aptana Cloud Page - apparently, they are not hosting Jaxer anymore ... cool! What should I do then? I know is there a way to go in the Jaxer page, but at the same time precedent signs let me think something truly bad happened to this project. Does anybody have any idea about it? What Is Going On To PECL For Windows We all know the " official site " is useless since ages: The pecl4win build box is temporarily out of service. We're preparing a new build system. I know PHP 5.3 put in core a lot of PECL libraries such mbstring, iconv, APC, APD, etc etc ... but I wonder when we will be able to add our favorite...

Template Engine - Why Bother? XML + XSLT

I am not sure I read the entire recent " war " in the PHP general mailing list about the best PHP Template Engine Option but, generally speaking, this argument is truly loads of solutions and controversial opinions. Here I am with mine. PHP 5 And In Core DOM, XML and XSLT Apparently, all these " Smarty adorers " did not consider that PHP 5 introduced a lot of core classes to work over DOM, XML, and transform them via XSL files. Nothing to do, they prefer to move an entire template application rather than use the best native, fast, in core, solution . Here a couple of reasons to generally prefer XML and XSL Transformations rather than whatever engine, specially those written in PHP itself, a not that fast interpreted programming language. Think Portable Apparently developers like to be stuck forever in a single programming language , something I cannot even try to consider, since Information Technology means Flexibility and Updates . An extremely valid point to cho...

PHP Full Proxy ... A Work In Progress

Something " truly dangerous " to play with: a proxy file able to understand everything via XMLHttpRequest, enabling any sort of cross site requests (no COMET yet, it could arrive soon). Dunno even why I am posting this " little monster ", but I am sure a lot of people would like to have " access to the entire web " simply using Ajax, and nothing else, something like this: function website_exists(url){ var xhr = new XMLHttpRequest; xhr.open("HEAD", "proxy.php?url=" + (url), false); xhr.send(null); return 199 }; if(website_exists("http://gogle.com")) alert("Of Course, It's Big G!"); WebReflection PHP Proxy <?php /** XMLHttpRequest PHP Proxy * @author Andrea Giammarchi * @blog http://webreflection.blogspot.com/ * @license Mit Style License * @requires curl and Apache webserver * @description basic authentication, GET, POST, HEAD, PUT, DELETE, others ...

re-introduction to JSON.hpack

Let me try again, this time with an official project page and a nearly complete wiki page . JSON.hpack, the Fat-Free Alternative to JSON JSON.hpack is a lossless, cross language, performances focused, data set compressor. It is able to reduce up to 70% number of characters used to represent a generic homogeneous collection. To understand how to use JSON via JavaScript, PHP, or C# (Python coming soon and hopefully other languages as well), please have a look into the wiki page or if you want, try out the demo page ! I am waiting for comments :-)

[PHP] Strict error on static call? Solved!

Just a quick post about Strict standard error when we have a class method which would like to behave differently if called statically. Apparently, and this is a truly old bogus I opened ages ago , the solution was to supress Strict errors (lol, thanks developers) even if in every other programming language, via overload, this kind of situation could be solved in a bit. How to solve the problem? While last post I suggested a couple of runtime solutions, this time I suggest to simply avoid the method definition and use a different one with a meaningful name. In this example I created a String class: class String { // choose a recognizable name for protected method static protected function _concat(array $arguments){ return new self(implode('', $arguments)); } // use magic __callStatic function static public function __callStatic($name, array $arguments){ // check the recognizable name switch($name = '_'.$name){ ...

[js.php] A JavaScript Object Like PHP Class

PHP 5.3 introduces the Closure class which is really useful and more powerful than good old lambdas via create_function . These are main limits of Closure class: you cannot serialize a Closure instance (and json_encode does not solve the problem at all) you cannot inject scopes via $this unless the closure comes from a class method While with an emulated prototype style class, something I tried months ago as well, the first point could not be a problem, to solve the second one is quite inevitable to use a Python like variable as first argument, called $self , to make the Closure both portable and re-adaptable. JSObject :: JavaScript Object Like PHP Class /** js.php basic JSObject implementation * @author Andrea Giammarchi * @blog WebReflection.blogspot.com * @license Mit Style License */ class JSObject implements ArrayAccess { // public static prototype static public $prototype; // ArrayAccess Interface public function offsetExists($index){return isSet(...

PHP to Jaxer Server And Vice-Versa

Basis If we have a configuration Jaxer + PHP, as is as example in my pampa-j project , we should know that these two languages cannot directly interact as is, for example, with Jaxer and Java. The interpreted and executed order is this one: the PHP module parses the request and the page before Jaxer module after PHP has finished its stuff, the Jaxer Server module parses the page which could has been modified during PHP execution after the Jaxer Server has finished its stuff, the user receives the generated page Above order means that we can pass variables to Jaxer but we cannot pass back results to PHP <?php // simple variable from PHP to Jaxer $myVar = 'Hello World'; echo '<script runat="server"> myPHPVar = '.json_encode($myVar).'; onload = function(){ // will show Hello World document.body.innerHTML = myPHPVar; }; </script>'; ?> As summary, if we print out a script tag wth a valid JavaScript for server, c...

PHP static as virtual self, what else for parent?

I find the static keyword in PHP 5.3 absolutely useful, but I instantly felt into a dilemma: what about parent? With static it is possible to refer to a generic class static property, without referring the class where the method has been declared. Here there is a simple example: class Numbers { public static $value = 0; public static function getValue(){ return static::$value; } } class One extends Number { public static $value = 1; } class Two extends Number { public static $value = 2; } $one = new One; $two = new Two; $one->getValue(); // 1 $two->getValue(); // 2 Above example shows how static behaves when an instance calls a method which uses static keyword. Whatever subclass it is, we will always obtain the defined value (if any, otherwise the inherited one) as expected. But what about parent behavior? If we use parent inside a method it is like using self, the parent keyword will always refers to the parent class, if any, of the method which conta...

png to gif transformation - an excellent free solution via PHP GD2

Why we still need png to gif in our web pages There a couple of valid plugins for different libraries able to manage " silently " png transparency in order to fix those browsers (cough: IE6) that are still used, for god knows which reason, but not able to understand properly the alpha channel. The problem is that these plugins cause overhead in our pages and for each png images, which is nearly a non-sense since these plugins fix old browsers whose performances have never been brilliant! Especially for modern GUIs, where little icons are widely used, these plugins could make the application incredibly slow. If we could choose old good GIF instead of png, 'till the end of those browsers, the problem will not exist and performances will be acceptable. PNG 2 GIF, simple? ... not, really! First of all a couple of search via Google brought me in " free but you have to pay anyway " applications through some open source, simple, but not that nice, program language solu...

PAMPA-J calls for tests

More downloads, and as usual more problems caused by different configurations. I would like to leave a single official 1.0 release in Google Code PAMPA-J Project page but I need your tests to do this. Test I would like to perform with your configurations: PAMPA starts (default configuration: Apache, MySQL, PHP, Jaxer) PAMPA works from every folder (Desktop, Documents, others) PAMPA closes processes on stop and/or exit PAMPA works from network as well (old version could be launched from another PC without problems and respecting paths) All above tests passed without problems for me, but I would like to be sure it works properly in other environments. Would you help me? Cheers :-)

PAMPA 1.0 - Jaxer included!

Update - Windows Home Fixed! Version 1.0b contains hot fixes for Window$ Home Edition plus some little change to make PAMPA more robust than before during process managment. If you already downloaded version 1.0 you can download only the Tray executable and rename it as PAMPA.exe Please tell me if you have still problems, hoping this b version will be the final 'till 1.1 ;-) Update Final Version 1.0 now served via Google Code ;-) I am proud to announce the first Release Candidate final 1.0 release of my tiny precious PAMPA , jumped from version 0.7 to 1.0 and rewrote from the scratch considering every suggestion its users gave me during these years (project born in 2006 and completely revisited for version 1.0) What is PAMPA-J PAMPA means P ortable A pache, M ySQL, and P HP A pplication, in this release with Aptana Jaxer included ! (shell I consider PAMPA-J a portable Aptana Cloud ?) What does it mean: Portable PAMPA is the first customizable and portable WAMP environment that...