web developing
28 Jun

There is an easy way to send requests with Zend_Http_Client either in GET and POST. Actually you can request not only with GET and POST methods, but with any other http request method.
$httpClient = new Zend_Http_Client('http://....'); // request via post $response = $httpClient->request(Zend_Http_Client::POST); // or get $response = $httpClient->request(Zend_Http_Client::GET);
However the interesting and useful thing is that you can perform authenticated requests:
$httpClient = new Zend_Http_Client('http://username:password@example.com'); // or define it later $httpClient = new Zend_Http_Client('http://example.com'); $httpClient->setAuth('username', 'password');
That’s an opportunity to send/receive authenticated POST requests.
$httpClient = new Zend_Http_Client('http://username:password@example.com'); // now post $httpClient->request(Zend_Http_Client::POST);
27 Jun

Every developer has been learned from his teachers how important is to comment his source code. You should comment the classes, the methods, the logic, etc. However nobody explained how exactly to code with comments between the lines. Have you ever seen that
That’s a shame! This doesn’t tell you anything, and what will happen if sometime later somebody sits in front of that code? He’d be amazed!
In fact the task is not to determine what are the bad parts of not commenting the code, but how to overcome this. How to write comments. Here are some basic advices.
First of all the most important thing is to write comments with other programmers in mind. They should understand the logic of your code from your comments and not from the code itself.
Even you’re not a native English speaker, and you English is not so good, it’s a must to write in English. This is really important when you work for a open source project when always there’s a big community around the project, you cannot be sure that everybody understands your language.
I know that this is difficult – also for me the English is not my native language, but that’s really a must. Once you start doing it, you’ll see that it’s not so difficult.
Don’t write in a method comment what technique is used to achieve the result. It’s good to describe what the method does, not how it does it.
Bad example:
/** * returns $a + $b */ public function sum($a, $b) { return $a + $b; }
Better:
/** * Returns the sum of two numbers * * @param number $a * @param number $b * @returns number $a + $b */ public function sum($a, $b) { return $a + $b; }
When there’s a loop or a statement it’s useless to describe that this is a loop or statement, isn’t it? A better approach is to describe in a comment why you needed to implement this logic in such way.
Wrong:
... $arr = array(); $i = 0; // in a while loop we // initialize every element with 0 while ($i < 100) { $arr[$i++] = 0; } ...
Better:
... $arr = array(); $i = 0; // we initialize an array of 0s // to store the sorted elements // on the first pass... etc, etc. while ($i < 100) { $arr[$i++] = 0; } ...
In fact the most important thing is to write comments with other developers in mind. Tell the future developer of that code what’s in the code, and pray for mercy
25 Jun
Yes! It’s really really fast, and it’s far quicker than the quicksort algorithm, which is considered as the fastest sorting algorithm in practice. However how it’s possible to be faster than the quicksort, which is the fastest algorithm?! Is that true? Actually it’s true, but only in few cases. It works with integers, you’ve to know the first and the last element from that set and you’ve to be sure that every element is unique
Imagine you’ve a set of numbers all of them greater than 1 and lesser than 1000. Of course you’re not suppose to have all of the integers between 1 and 1000, but only few of them – think of 500 numbers between 1 and 1000! Here’s important to note – that this is only an example, you can have far more than only few numbers between 1 and 1000 – what about the numbers between 1 and 1,000,000 – this is a big set, isn’t it.
The question is – if there are so many constraints, why should I use that algorithm instead of quicksort, or another sorting algorithm, that works with everything. The answer is clear – yes, you’d prefer quicksort if you’ve to sort some arbitrary data, but when it comes to integers, and you’ve, let’s say, 1,000,000 integers, my advice is – use this algorithm!
First we have an unsorted array, but we know the minimum and maximum of the set.
On the first pass initialize an empty array with as many elements, as they are between the first and the last element of the set – for a set between 1 and 1000 – that will be an array with 1000 elements – each of which will be a zero in the beginning.
Than loop trough the set and for every element in the set – you should put a 1 on it’s place
Now we have an array of 0 and 1.
After the first pass, you’d guess what you’ve to do – loop trough the second array and print the keys of the elements different from 0 – those that are 1.
Now the array is sorted!
var a = [34, 203, 3, 746, 200, 984, 198, 764]; function setSort(arr) { var t = [], len = arr.length; for (var i = 0; i < 1000; i++) { t[i] = 0; } for (i = 0; i < len; i++) { t[arr[i]] = 1; } for (i = 0; i < 1000; i++) { if (1 == t[i]) { console.log(i); } } } setSort(a);
Now that you’ve seen that algorithm, perhaps you’d guess that it’s no so difficult to change from integers to any other set, and once again I should say that in many cases this is the best algorithm for sorting! Very often quicksort is preferred, but not always there isn’t something faster!
12 Jun
Recently I started managing a small system based on Joomla 1.0.15. I know there is a most recent version of the popular CMS, but this one wasn’t based on it. Actually once upon a time I was working with Joomla as basic CMS for my web projects, but now after two+ years with Zend Framework and MVC I was amazed to see how bad the Joomla 1.0.15 was written.
Indeed it is really strange to see all SQL, HTML and PHP all in one file?! This is like end of ’90s! That’s for sure a bad approach.
I’ll download the 1.5 version with the hope the code there will be better!
11 Jun
Here’s some Friday fun. Let me show you one sorting algorithm, perhaps the most known of all them – the quick sort, implemented both on PHP and JavaScript. Although the code look similar between both languages, there are few differences, that show the importance of the syntax knowledge!
<?php $unsorted = array(2,4,5,63,4,5,63,2,4,43); function quicksort($array) { if (count($array) == 0) return array(); $pivot = $array[0]; $left = $right = array(); for ($i = 1; $i < count($array); $i++) { if ($array[$i] < $pivot) $left[] = $array[$i]; else $right[] = $array[$i]; } return array_merge(quicksort($left), array($pivot), quicksort($right)); } $sorted = quicksort($unsorted); print_r($sorted);
var a = [2,4,5,63,4,5,63,2,4,43]; function quicksort(arr) { if (arr.length == 0) return []; var left = new Array(); var right = new Array(); var pivot = arr[0]; for (var i = 1; i < arr.length; i++) { if (arr[i] < pivot) { left.push(arr[i]); } else { right.push(arr[i]); } } return quicksort(left).concat(pivot, quicksort(right)); } console.log(quicksort(a));
Note that the first conditional statement is quite important! While in PHP the count function will return 0 either on a NULL value or an empty array and you can substitute it with something like count($array) < 2
if (count($array) < 2) return $array;
in JavaScript you cannot use that because of the presence of the ‘undefined’ value when an “empty” array is passed as an argument. Thus you’ve the conditional above:
// this will result with an error if (arr.length < 2) return arr;
An iterative version of the algorithm next Friday!