tags:

views:

1627

answers:

2

hi i forgot the code which in a sample class you have to add so that it runs automatically?

is it wakeup or something?

like so:

class something {
 function automaticxxx_something_which_runs when class is created()
 {
 }
}

$s = new something();

-what do i create in the class file so that something runs already after the class is initialized?

i forgot how to name the function name so that it would call it automatically the first function.

+3  A: 

You're after a constructor. In PHP4 the method has the same name as the class

class Foobar
{
    function Foobar()
    {
        echo "Hello World!\n";
    }
}

new Foobar()

In PHP5 the above method still works, but the correct way is to use the __construct() method

class Foobar
{
    function __construct()
    {
        echo "Hello World!\n";
    }
}

new Foobar();
Matthewd
+3  A: 

If you want a constructor that works in both versions ( although, you should not be coding for php4 as its well past its end-of-life now )

class Foobar
{
    function __construct()
    {
        echo "Hello World!\n";
    }
    function Foobar() 
    {
        return $this->__construct();  
    }
}

If you are coding for Just php5 you should get into the habit of specifying visibility explicitly,

class Foobar 
{
    public function __construct() 
    { 
    }
}

(visibility definers didn't exist back in php4)

Should do the trick, with a minor performance loss under php4.

Kent Fredric