Home
About
Search
🌐
English Română
  • PHP 5.4 Alpha 1 is here!

    Citește postarea în română

    Jul 1, 2011 closures php5.3 php5.4 php6 scalar type hinting traits
    Share on:

    Three days ago, that is on 28-06-2011 the PHP 5.4 alfa 1 version was announced on ww.php.net!

    Basically in this release are the things that were made for PHP 6 and did not make it in PHP 5.3, next to some other new features.

    Some of the most interesting new features are:

    Traits

    A new OOP feature. Basically for horizontal code reuse, that is inheriting of methods instead of extending classes.

     1trait Inharitable {
     2    public function test() {
     3        echo 'Class: ' . __CLASS__ . ' Method: ' . __METHOD__ . PHP_EOL;
     4    }
     5}
     6
     7class A {
     8    use Inharitable;
     9}
    10
    11class B {
    12    use Inharitable;
    13}
    14
    15$a = new A();
    16$a->test(); //Class: Inharitable Method: Inharitable::test
    17
    18$b = new B();
    19$b->test(); //Class: Inharitable Method: Inharitable::test
    

    Traits in PHP 5.4 are the new namespaces of PHP 5.3, that is the most interesting feature in PHP 5.4.

    Scalar type hinting

    Up to PHP 5.3 there was type hinting only for classes, interfaces and arrays. With PHP 5.4 type hinting can be used for scalar data types like: int, string, float, book and resource.

    1function test(string $var) {
    2  echo $var;
    3}
    4
    5$a = 'aa';
    6test($a);
    

    Unfortunately on this alpha version on my computer I get: Catchable fatal error: Argument 1 passed to test() must be an instance of string, string given, called in .. on line 58 and defined in … on line 52

    What can I say… it is still an alpha…

    Closures

    Yes, I know, there are closures in PHP 5.3 too, but there are not the same. In PHP 5.3 if you wanted a closure you had to use the keyword use and then specify the variables that the lambda functions will have access to.

    In PHP 5.4 it’s beginning to look more like JavaScript, in a good way:

     1class closureTest {
     2
     3    private $a;
     4
     5    function test() {
     6        $this->a = 'object var';
     7        return function () {
     8            echo $this->a;
     9        };
    10    }
    11}
    12
    13$a = new closureTest();
    14$b = $a->test();
    15$b(); // object var
    16unset($a);
    17$b(); // object var
    

    Closure in the right way, with a lambda function the way it should be! Just like lambda functions existed even before PHP 5.3, but only after the new syntax they’ve become popular, now there was closures time.

    This are some of the things that I find most interesting, but there are only a part of the new features that PHP 5.4 brings!

    It’s likely that before the end of this year the final version will be ready.

    I’m curious if with the final version of PHP 5.4 a new certification will come out, taking in consideration that the changes are not major.

  • Yahoo! Open Hack day Europe 2011 in Bucharest – review

    Citește postarea în română

    May 24, 2011 Douglas Crockford hack jquery node.js Yahoo Yahoo Open Hack yui
    Share on:

    After a couple of days of almost no sleep, about 6 Red Bulls and an app, Yahoo Open Hack Day Europe 2011 was over.

    Initially I only wanted to go to tech talks. Later I though that I should go as hacker because I didn’t what to miss anything from the event, but not to present a project. Ultimately I was chosen to present the project.

    Everything began at 7 am, when a colleague came to pick us up in time to have a cup of coffee before the event. We were 6 attendees so we thought that we can split into two teams.

    At about 9 am we’ve arrived at Crystal Palace Ballrooms. We were greeted by the “crew” in green t-shits, who have guided us to the registration offices.

    Once registered we started looking for a table in the room where all 6 of us can sit. In the room there were about 350 people who came for both hacking and tech talks.

    In the middle of the room there was a table reserved for experts and Yahoo! staff. At some point a colleague tells me: “isn’t that Crockford?”. Indeed he was at the reserved table Douglas Crockford, along with many other important Yahoo! figures. I’ve rushed to get my book (JavaScript: The Good Parts) which I’ve brought specially for this event hoping to get an autograph.

    Nervous, I approached his table: “Mr. Crockford?”, he saw the book and smiling he answered “Hey, my book!”. Nervously I gave him a pen and asked him to give me an autograph. I know it sounds ridiculous, but for me to meet someone of DC stature, about who I’ve only saw presentations at Google and Yahoo! and read his book, to meet face to face, to have the opportunity to ask for an autograph  and shake his hand was like meeting a famous actor.

    I’ve returned to our table and I’ve bragged to everyone with my autograph, I thought that the first item on my agenda was achieved.

    The tech talks began:

    • “Hacking 101” by Murray Rowan
    • “YQL and other Y! APIs” by Jon Leblanc
    • “YUI” by Luke Smith
    • “node.js” by Reid Burke
    • “Introduction to Yahoo Messenger API” by Vivek V Aggarwal
    • “Server Sidedness” by Doug Crockford

    Node.js and YQL were the most fascinating talks for me, because I haven’t used neither of the technologies. I find node.js very interesting for JavaScript games, where communication is very dynamic and PHP is not very suitable. On the other hand, if I knew of YQL’s existence it would have helped me get rid of a lot of work so far.

    After the tech talks the hacking started. Two of my colleagues were going to make an app to clean the page of anything but relevant content. The remaining four of us were a little puzzled because we had no ideas. What was certain is that we wanted to use node.js or YQL. We started installing node.js on our laptops and see what it can do. Somewhere along the way the idea came, a multi-player snake game which we called “Tequila Worms“. I had to make the UI because I had more expericence with JavaScript games. Initially I wanted to reuse the code base from my snake game. But there was a small problem, I didn’t have the original source code with me. On the other hand it was my first JavaScript game, so it wasn’t exactly “beautiful” written. At that point I decided to start from scratch using canvas.

    I was very happy to work on an app without thinking at backwards compatibility.

    At about 18:30 there was another talk by Douglas Crockford “The JSON Saga”. I saw this talk before on developer.yahoo.com so I thought is not going to be very exciting for me. But it turns out it was quite different. An interesting part was when one of the slides said “IE6 MUST DIE!”, a very good point, but then… “IE7 MUST DIE!”, then 8 and 9. At some point I was wondering why didn’t he simply write “IE MUST DIE!”.  The answer came on the next slide “IE10 MAY LIVE!”. IE10 is the only Microsoft browser that implements “ECMAScript 5: Strict Mode”.  The recorded talk can be found at: http://www.youtube.com/watch?v=NXiAKPTDXk.

    After the talk I’ve asked Douglas Crockford to take a photo with me, one more reason for me to brag that I was to this event!

    This talk was the last one until the end of the hackathon.

    At about 19:30 it was the entertainment part, sorting algorithms on traditional romanian music:

    After that was dinner and a very long night.

    After several hours of intensive coding we made a visit to the games room, where waiting for us were: a XBOX,  a WII, a Ping-Pong table, Fuzzball table and a pool table. During the night we made serveral visits to the game room to relax and stretch after sitting too long on the purple bean bags.

    Sometime after midnight the other team dropped the project because they got stuck in YUI, so only our team remained active.

    After 3 am few colleagues began to fall asleep on the bean bags, but in my team sleep was not an option. To prevent me from sleeping I drank 2 or 3 RedBulls. The project was starting to take shape and I said to my self that I will not sleep before we have the first version running.

    At about 5:30 am I blinked from a moment and felled asleep with my hands still on the keyboard, for about an hour. One of the most relaxing nap I ever took in my life.

    In the morning a new espresso machine appeared to keep us alive for the remaining time, so we continued with new strength.

    After getting to a working version it was more of a continuous playing with small brakes to fix the bugs that we found along the way. I think that it seemed fun enough that even Ted Drake came and had a game with us.

    After 24h of hacking the end was announced and we had to go and register. I was going to present, I was overwhelmed by the moment. I took a number, set up my monitor and went to eat.

    We has a little issue, the game was working fine using websockets over local network and we didn’t know how we were going to do the presentation. After some running around we managed to figure out the details but not the presentation. I realized I was in front of 300 people with no speech and with a game made literally over night. I finished very quickly and I can’t really remember what I said, the only things I could see was the spotlights in my eyes and I was so nervous that I thought I was going to faint. But I think everything turned out ok.

    1.5h of presentations by 50 teams followed.

    While the jury deliberated we went outside and had a brief conversation with Luke Smith, and a not so brief conversation with Reid Burke which during the night helped us with our project. We’ve started our conversation with Reid on node.js and ended with YUI and why is not very popular. I found out that Reid is only 22 years old and it’s been with Yahoo! for 3 years already. At some point we told Reid that we didn’t like YUI because it has such a different approach then jQuery, but he openly answered all of our questions explaining why they approach things this way. Maby if YUI would better highlight why there doing some things so different it would become a much popular framework.

    During the awards Anil Patel (the event host) insisted that presenters should stay on the bean bags in front. And because the jury was getting late Anil told us some funny stories about what happend at other Yahoo! Open Hack events. And because the jury still wasn’t coming back from deliberation he sang us some Abba.

    Winners were announced:

    • Best Messenger Hack: Yahoo! Social Programming – Corabia Nebunilor
    • Best Yahoo! product enhancement: TheBatMail – The BatMen
    • Best Hack for Social Good: Map of Deforested Areas of Romania – We plant good deeds in Romania!
    • Best Mashup: Ymotion – The Guys
    • Best Local Hack – Tourist Guide – Javascript Team
    • Best BOSS Hack – Take a Hike – Plan 9
    • Hacker’s Choice – The Yahoo! Farm – “Cloud Data in Your BedRoom” – The RoboFun Team
    • Best in Show – Pentru 3 mashup-uri: Take a Hike, Quick info, Playlist-O-Matic – Plan 9 – Alexandru Badiu

    After the awards we found out why it was so important to sit on the bean bags, presenters would take the bean bags home!

    The after-party followed, but we were to tired, so after smoking a cigarette and drinking a cup of wine we ended our trip at Yahoo Open Hack Day Europe 2011!

    As a conclusion, the accommodations were almost perfect, no detail was left to chance. I overheard a discussions that they even prepared umbrellas in case the weather would turn bad.

    __

  • Android calculator vs. JavaScript

    Citește postarea în română

    May 3, 2011 Android JavaScript
    Share on:

    Is about the phone’s calculator. I have a HTC Desire with Android 2.2. My default calculator app that’s called simply “Calc” amazes me. Very rarely happens to need to use it, end even less I’m using it with fractional numbers, but when I’m actually using it I tend to forget that it has an issue.

    Let’s say:

    12 – 11

    In Calc = 1, in JavaScript = 1. Nothing out of the ordinary so far, right?

    Let’s take:

    1.2 – 1.1

    Any elementary school student knows that the result is 0.1.

    In Calc it is 0.099999999, weird? In JavaScript is even more interesting, the result is 0.09999999999999987.

    It’s about float, and I assume that the difference comes from the digit number displayed on the screen. In both cases IEEE 754 standard is used.

    Now… in JavaScript this is a known issue, but who made the Android app, made it transform to a scientific calculator if you rotate the phone and did not see this issue?

  • Yahoo! Open Hack Europe 2011 at Bucharest!

    Citește postarea în română

    Apr 21, 2011 hack Yahoo
    Share on:

    Yahoo! Open Hack Europe 2011 will be held in Bucharest! Between 14-15 May we are the lucky Europeans to host this event.

    At this event there are two types of tickets: Tech Talk and Hacker. Those who choose Tech Talk will attend seminars and awarding only, while those who choose Hacker will stay overnight to work on the project they’ve chosen.

    Important figures from the Yahoo! staff will be present, so in short is an event that you must not miss!

  • Closures and lambda functions in PHP vs. JavaScript

    Citește postarea în română

    Apr 16, 2011 anonymous functions closures JavaScript lambda PHP php5.3
    Share on:

    JavaScript and PHP support both lambda functions and closures. But the terms are poorly understood in both programming languages ​​and are often confused with each other.

    Lambda functions

    Also called anonymous functions. They refer to functions that can be called without being bound to an identifier. One of their purposes is to be passed as arguments. The Lambda name was introduced by Alonzo Church, inventor of lambda calculus in 1936. In lambda calculus all functions are anonymous.

    JavaScript

    In JavaScript lambdas are part of the standard set and there are the preferred method of defining functions.

    For instance:

    1var add = function (a, b) {
    2     return a + b;
    3}
    4alert(add(1, 2)); // 3
    

    Lambda functions are used almost in any context when it comes to JavaScript, like:

    1window.onload = function (e) {
    2     alert('The page has loaded!');
    3}
    

    PHP

    In PHP, lambda functions were introduced in version 4.0.1 using create_function. In version 5.3+ a similar syntax to JavaScript was added, a much more readable and elegant way of defining a function.

    This means that in PHP there are two ways of creating a lambda function:

     1// PHP 4.0.1+
     2$add = create_function('$a, $b', 'return $a + $b;');
     3
     4// vs.
     5
     6// PHP 5.3+
     7$add = function ($a, $b) {
     8     return $a + $b;
     9};
    10
    11echo $a(1,2); // 3
    

    Lambda functions can be used as parameter for other functions, such as usort:

    1$array = array(4, 3, 5, 1, 2);
    2usort($array, function ($a, $b) {
    3     if ($a == $b) {
    4          return 0;
    5     }
    6     return ($a < $b) ? -1 : 1;
    7});
    

    Even more, PHP 5.3+ allows calling an object as a anonymous function:

    1class test {
    2     function __invoke($a) {
    3          echo $a;
    4     }
    5}
    6$a = new test();
    7$a('test'); // 'test'
    

    Closures

    The closure is really the misunderstood concept of the two. In general confusion appears because closures may involve lambda functions. A closure refers to the ability of a function/object to access the scope in which it was created even if the parent function has ended it’s execution and returned. In other words, the function/object returned by a closure is running in the scope in which it was defined.

    In JavaScript the notion of closure is part of the standard arsenal, because the language is not based on the traditional object model, but rather on prototypes and functions. But JavaScript has some traditional object model parts, like the fact that you can use “new” to construct an object based on a function that plays the role of a class. In PHP closures are more of an new way to approach problems, because PHP is part of the traditional object model family.

    JavaScript

    In JavaScript the notion of closure is widely used, it’s so popular because JavaScript is not a traditional object orientated language, but rather a functional one, based on prototype inheritance.

    JavaScript doesn’t have Public, Private and Protected, but rather only Public and Private and objects an inherit from each other, without using classes.

    Another issue is the scope, because the global scope is used by default. This issues can be fixed in an elegant fashion using closures:

     1var closure = function () {
     2     var sum = 0;
     3     return {
     4          add: function (nr) {
     5               sum += nr;
     6          },
     7          getSum: function () {
     8               return sum;
     9          }
    10     }
    11}();
    12
    13closure.add(1);
    14closure.add(2);
    15console.log(closure.getSum());
    

    In the example above, sum is a private property and in theory can only be accessed and modified by the closure function. The interesting part is that the parentheses from the end of the function definition, signify that this function will be immediately  executed and therefore will return the result which is an object. At this point the original function will only exist for serving the return object, encapsulating therefor the private variable.

    Although the function has finished execution, through this closure the returned object can still access the variables defined in the function scope, because that was the environment in which it was created.

    This becomes even more interesting when a function returns another function:

     1var counter = function () {
     2    var counter = 0;
     3    console.log('in closure');
     4    return function () {
     5        console.log('in the anonymous function');
     6        return ++counter;
     7    };
     8};
     9var counter1 = counter();
    10
    11console.log(counter1()); // 1
    12
    13var counter2 = counter();
    14console.log(counter2()); // 1
    15console.log(counter1()); // 2
    

    The output will be:

    1in closure
    2in the anonymous function
    31
    4in closure
    5in the anonymous function
    61
    7in the anonymous function
    82
    

    What actually happens is that the first function is executed and returns an anonymous function that can still access the environment in which it was created. In my opinion this is where the confusion between closures and lambda functions comes from, because a function returns another function.

    The difference between examples is that in the first one the closure function executes immediately, and in the second example when counter is executed it’s returning a result that is actually a function definition, which in turn can be executed. Of course the second example can be modified to act just like in the first example using parenthesis.

    PHP

    As I said above, the notion of closure in PHP is not as important as in JavaScript.

    Considering that lambda functions are available in the language since version 4, closures only appeared with PHP 5.3+.

    Because of the block scope nature of PHP, there is a better encapsulation but there is a lot less flexibility compared to JavaScript. Basically in PHP you must specify using the use instruction what will the anonymous function be able to access from the closure scope.

     1function closure () {
     2     $c = 0;
     3     return function ($a) use (&$c) {
     4          $c += $a;
     5          echo $a . ', ' . $c . PHP_EOL;
     6     };
     7}
     8
     9$closure = closure();
    10
    11$closure(1);
    12$closure(2);
    

    Unlike JavaScript, in PHP closures can not return objects, or rather the object can not be bound to the scope in which it was created, unless you send the variables as a reference to the constructor, in which case is not very elegant and I can’t imagine a scenario that would absolutely need closure for this.

    Like in the JavaScript examples, instead of parentheses “()” at the end of the function, in PHP to run a function immediately after defining it call_user_func() or call_user_func_array() can be used:

     1$closure = call_user_func(function () {
     2    $c = 0;
     3    return function ($a) use (&$c) {
     4        $c += $a;
     5        echo $a . ', ' . $c . PHP_EOL;
     6
     7    };
     8});
     9
    10$closure(1);
    11$closure(2);
    
    • ««
    • «
    • 7
    • 8
    • 9
    • 10
    • 11
    • »
    • »»

Claudiu Perșoiu

Programming, technology and more
Read More

Recent Posts

  • Adding a slider to Tasmota using BerryScript
  • The future proof project
  • Docker inside wsl2
  • Moving away from Wordpress
  • Custom path for Composer cache
  • Magento2 and the ugly truth
  • A bit of PHP, Go, FFI and holiday spirit
  • How to make use of the Xiaomi Air Conditioning Companion in Home Assistant in only 20 easy steps!

PHP 49 MISCELLANEOUS 46 JAVASCRIPT 14 MAGENTO 7 MYSQL 7 BROWSERS 6 DESIGN PATTERNS 5 HOME AUTOMATION 2 LINUX-UNIX 2 WEB STUFF 2 GO 1

PHP 35 JAVASCRIPT 15 PHP5.3 11 MAGENTO 7 PHP6 7 MYSQL 6 PHP5.4 6 ZCE 6 CERTIFICARE 5 CERTIFICATION 5 CLOSURES 4 DESIGN PATTERNS 4 HACK 4 ANDROID 3
All tags
3D1 ADOBE AIR2 ANDROID3 ANGULAR1 ANONYMOUS FUNCTIONS3 BERRYSCRIPT1 BOOK1 BROWSER2 CARTE1 CERTIFICARE5 CERTIFICATION5 CERTIFIED1 CERTIFIED DEVELOPER1 CHALLENGE1 CHM1 CLASS1 CLI2 CLOSURES4 CODE QUALITY1 CODEIGNITER3 COFFEESCRIPT1 COLLECTIONS1 COMPOSER1 CSS1 DEBUG1 DESIGN PATTERNS4 DEVELOPER1 DEVELOPMENT TIME1 DOCKER2 DOCKER-COMPOSE1 DOUGLAS CROCKFORD2 ELEPHPANT2 FACEBOOK2 FFI1 FINALLY1 FIREFOX3 GAMES1 GENERATOR1 GO1 GOOGLE1 GOOGLE CHROME1 GOOGLE MAPS1 HACK4 HOMEASSISTANT2 HTML2 HTML HELP WORKSHOP1 HTML51 HUG1 HUGO1 INFORMATION_SCHEMA1 INI1 INTERNET EXPLORER3 IPV41 IPV61 ITERATOR2 JAVASCRIPT15 JQUERY1 LAMBDA1 LINUX1 MAGENTO7 MAGENTO22 MAP1 MINESWEEPER1 MOTIVATION1 MYSQL6 NGINX1 NODE.JS2 NOSQL1 OBSERVER3 OBSERVER PATTERN1 OOP1 OPERA1 OPTIMIZATION1 ORACLE1 PAGESPEED1 PAIR1 PARSE_INI_FILE1 PHONEGAP2 PHP35 PHP ELEPHANT2 PHP FOR ANDROID1 PHP-GTK1 PHP42 PHP53 PHP5.311 PHP5.46 PHP5.53 PHP5.61 PHP67 PHP7.41 PROGRAMMING1 REVIEW1 ROMANIAN STEMMER2 SAFARY1 SCALAR TYPE HINTING1 SCHEME1 SET1 SHOPPING CART PRICE RULE1 SINGLETON1 SOAP1 SPL2 SQLITE1 SSH1 STACK TRACE1 STDERR1 STDIN1 STDOUT1 SUN1 SYMFONY2 TASMOTA1 TEST TO SPEECH1 TITANIUM2 TRAITS1 TTS1 UBUNTU1 UNICODE2 UTF-82 VECTOR1 WEBKIT1 WINBINDER1 WINDOWS2 WORDPRESS1 WSL21 YAHOO3 YAHOO MAPS1 YAHOO OPEN HACK1 YSLOW1 YUI1 ZCE6 ZCE5.31 ZEND3 ZEND FRAMEWORK3
[A~Z][0~9]

Copyright © 2008 - 2024 CLAUDIU PERȘOIU'S BLOG. All Rights Reserved