Jump to content

run function when class is created


ratcateme

Recommended Posts

I am making an IRC admin bot. i am new to using OOP and i am using it for my bot.

i want to call a script that will connect to the server and open a MYSQL connection when the class is called.

is this even possible.

 

Scott.

Link to comment
Share on other sites

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!

 

}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.