ratcateme Posted March 9, 2008 Share Posted March 9, 2008 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 https://forums.phpfreaks.com/topic/95170-run-function-when-class-is-created/ Share on other sites More sharing options...
numan82 Posted March 9, 2008 Share Posted March 9, 2008 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 https://forums.phpfreaks.com/topic/95170-run-function-when-class-is-created/#findComment-487565 Share on other sites More sharing options...
Naez Posted March 9, 2008 Share Posted March 9, 2008 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 https://forums.phpfreaks.com/topic/95170-run-function-when-class-is-created/#findComment-487569 Share on other sites More sharing options...
ratcateme Posted March 10, 2008 Author Share Posted March 10, 2008 thanks i think ill use Naez method Scott. Link to comment https://forums.phpfreaks.com/topic/95170-run-function-when-class-is-created/#findComment-488219 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.