Jump to content

run function when class is created


ratcateme

Recommended Posts

hi scott,

yeah the function can be called from the class being created. and it can done like this

 

class MyClass{

 

  function DoSomeThing($host,$username,$pass){

 

  //your code for mysql connections come here like mysql_connect etc...

 

  }//function ends here

}//class ends here

Now you can create the object of this class and can pass the host,username and password to connect to mysql

For example

 

$ConnectorObj=new MyClass();

$ConnectorObj->DoSomeThing($host,$username,$pass);

 

Note: Don't forget to include the class php where you are using the class like include('MyClass.php');

Thanks!

 

}

name the function __construct() if you want it to be run on object creation.

 

for instance

 

class myClass
{
    public function __construct($var)
    {
      // do stuff on creation, you can specify parameters as shown
      echo $var;
    }
}

$myObject = new myClass("yezzir"); // will output yezzir

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.