Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

object

oop

php

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.

like image 206
jpalala Avatar asked Dec 31 '22 08:12

jpalala


1 Answers

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.

like image 119
Kent Fredric Avatar answered Jan 13 '23 15:01

Kent Fredric