Sunday, May 8, 2011

PHP Declaring Object before Defining Class

A quiz for you. is the following code snippet valid?
    $obj = new hello(); 

    class hello{

        function __construct(){
        echo 'PHP ';
        }
      }

    

What about the following one?


     hello();

     function hello(){
           echo 'PHP ';
     }


what about the next? assuming test.php contains the hello function.

     hello();

    include test.php;


Unlike C/C++/Python The first two snippets are correct. But the third one will generate error. When you call a function or make an instance of a class the compiler searches in the same file for the definion.

No comments:

Post a Comment

About Me

Web Developer From Dhaka, Bangladesh.