Archive for March 30th, 2007

Linking to localhost?

Ever wondered how many people are linking to localhost files from their websites or blogs? Today, I found out an easy way to get the latest localhost linkers!

I installed wordpress locally and was testing it using the URL http://localhost/. The incoming links section of the Wordpress dashboard showed the following blogs which are linking to localhost. Very funny!

Localhost linking!

If you check your localhost installation, you might find my blog as well! Pretty neat way to get more traffic - eh?

Internet Explorer 7 rocks!

Internet Explorer 7.0Today I upgraded Internet Explorer from version 6 to the latest version 7. The download requires genuine windows validation 3 times! Apart from that that 20MB download went smooth.

This review is based on my experience of using IE for just one hour! IE 7.0 simply rocks! The interface is sleek and the performance is unbelievable (for a microsoft product, that is  :-)) . Here are some of the interesting features of Internet Explorer 7.0,

1. Improved screen rendering. Now reading a webpage seems easier probably due to enhanced anti-aliasing.

2. Minimal, but effective UI - The UI is similar to Windows media player and is quite intuitive. All the frequently used features are easily accessible.

3. The tabbed browsing - This was probably the only reason I was using Firefox. IE 7.0 contains this feature and is better than Firefox when it comes to opening new tabs (new tab appears just near the old tab)!

IE 7.0 tabs

4. Reduced startup time - I have a 1GB, 1.8GHZ machine and IE 7.0 starts in a flash!

Another noticeable feature is the Live search which appears on the right of URL field. It is quite tempting to use it instead of Google, nice try Microsoft! :)

So overall, I am very much pleased with IE 7.0. If you are using IE 6.0, I recommend immediately upgrading it to IE 7.0 (If you have genuine windows  :-))

PHP notes for Java programmer (4) - classes

Here is the last set of PHP notes which cover object oriented programming in PHP. The notes conclude with a set of supplementary notes. The entire list of PHP notes are available here.

PHP Notes - Object oriented programming

1. To call class methods without an instance, use scope resolution operator(::) else use (->). Check the following example,

class test {
 function f1() {
  echo “hello”;
 }
}

test::f1();

$t1 = new test();
$t1->f1();

2. In PHP, __autoload function (automatically called) can be used to simulate “import” functionality. This is very handy if you store your classes in separate PHP files. For example,

<?php
function __autoload($class_name) {
    require_once $class_name . ‘.php’;
}

$obj  = new MyClass1();
$obj2 = new MyClass2();
?>

3. PHP has constructors (__construct()) and destructors (__destruct()). There is no chaining of constructor or destructor calls. Use parent::__construct() for the same.

4. Method and properties can have “public”, “private” or “protected” visibility.

5. Use const keyword to define constants inside classes.

6. PHP has the standard class constructs - abstract, extends, interface, implements etc.

7. It is possible to overload method calls and member access using __call, __get and __set. This will be triggered only if the accessed attribute or method doesn’t exist! isset() and unset() can also be overridden using __isset() and __unset().

8. For serialization methods __sleep and __wakeup. All methods starting with __ are reserved as magic methods in PHP!

9. To perform shallow copy, use clone directive. It is also possible to define a __clone() method.

10. PHP supports reflection paradigm. For more details, checkout http://www.php.net/manual/en/language.oop5.reflection.php

11. PHP supports type hinting for object and array types. For example, function f1(array $a) { }

12. A typical PHP class declaration is shown below,

class Test extends T1 implements T2 {

 public $a;
 
 public function testmethod($value) {
  $this->a = $value;
 }
}

13. PHP exception handling is similar to Java. It is also possible to extend the base “Exception” class. Here is a sample use of exception,

try {
    $error = ‘error message’;
    throw new Exception($error);
} catch (Exception $e) {
    echo ‘Caught exception: ‘,  $e->getMessage(), “\n”;
}

PHP supplementary notes

1. For maximum security always validate user input. You don’t want to be a victim of SQL injection attack!

2. For database access, provide least access rights needed by the application.

3. On a production system it is better to disable error reporting.

PHP notes for a programmer - 10 minutes quick tutorial

Following is a set of notes I made during my study of PHP programming language. This assumes that you are familiar with one of the OOP languages (Java, C++ or C#). I hope this would be really helpful if you are looking for a 10 minute quick tutorial for PHP!

0. Starting PHP development

1. Introduction to PHP

2. PHP types and variables

3. Control structures and functions in PHP

4. Object oriented programming in PHP

Tutorials

In this page you will find all the tutorials I have written in my blog. All the posts are grouped here based on the topic. This is continously updated so stay tuned for more tutorials!

1. PHP Notes for a practising programmer

PHP notes for Java programmer (3) - control structures and functions

Here is the 3rd set of PHP notes. With this all the core PHP language features are covered. The only thing left is the OOP support in PHP - classes and interfaces. For classes, I will cover only the PHP 5 features (PHP 4 has a different model). Stay tuned for the final set.

PHP Language Reference Notes - control structures and functions

1. There are two equality operators in PHP. == is for “equals” and === is for “identical”. “===” return true only if both operands are of the same type!

2. @ is an error control operator in PHP. This works only on expressions and will suppress error messages. Handy if you don’t want to expose errors to the page being displayed.

3. Backticks (“) can be used as the execution operator in PHP. for example $output = `ls`; will contain the directory listing in linux.

4. There are two types of logical operators - “and”,”&&” , “or”, “||”. The “or” operator has less precedance than “||”.

5. In arrays, “+” operator indicates union. “==” checks whether same key/pair value exist in arrays, while “===” also checks for their order (identical).

6. Following are the control structures in PHP,

if ($a > $b) {
   echo “a is bigger than b”;
} elseif ($a == $b) {
   echo “a is equal to b”;
} else {
   echo “a is smaller than b”;
}

while ($i <= 10) {
   echo $i++;
}

do {
   echo $i;
} while ($i > 0);

for ($i = 1; $i <= 10; $i++) {
   echo $i;
}

switch ($i) { // switch can work on strings!
case “apple”:
   echo “i is apple”;
   break;
case “bar”:
   echo “i is bar”;
   break;
case “cake”:
   echo “i is cake”;
   break;
}

7. There are two forms of else if in if blocks. It can be “else if” or “elseif”.

8. All control structures has an alternate form which uses colon(:) and end<type>; For example, if($a): endif;

9. PHP has a shortcut form of for, the foreach. Consider the following example,

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
   echo $value;
}
?>

10. The break statement takes an optional parameter indicating how many levels needs to be skipped. For example, break 2;

11. The continue statement takes an optional parameter indicating how many levels needs to be skipped when going to the next iteration. For example, continue 2; Also note that continue applies to switch statement as well!

12. The declare construct can be used for profiling. For example, declare(ticks=1) { //code here }. This works along with register_tick_function(). The registered function will be called for every n low level instructions, where n is the number of ticks.

13. To include another PHP file use either include() or require() construct. require() will cause a fatal error if the required file is missing. To avoid multiple includes of the same file, use include_once() or require_once().

14. In PHP, it is possible to define functions inside functions! Function visibility is global but the outer function must be called before you can call the inner function!

15. There is no function overriding or overloading in PHP.

16. Function arguments can be passed by value or reference. To pass by reference prefix variable with &. PHP also supports variable function arguments.

17. Default function argument values are possible and they should be ordered from right to left. For example,

function test($a, $b=”value”) {
}

18. PHP also has variable functions. This can be used for implementing function callbacks!

function f1() {
 echo “hello”;
}
$functionvar = “f1″;
$functionvar();