how to object initialize in PHP and then what function do I make within the class which runs automatically?

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.

Answers


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();

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.


Need Your Help

Windows Forms graphics not being drawn

c# winforms graphics

I have some code that looks pretty simple and should draw an ellipse, but it doesn't seem to appear. Here is my code:

UAC and Windows box selections in Windows

python windows com

So I'm using Python (though another language suggestion like C# or VB is fine too). I want to have a program launch an EXE file installer, and then tell that installer that it is alright to run the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.