Page 1 of 1

Quick OOP Question

Posted: Fri Sep 30, 2005 6:14 pm
by Sphenn
Hey everybody,

Just a quick question.

Does anyone know if it's possible to pass arguments to a class while initializing it?

ie

Code: Select all

$db = new db($username, $password, $etc);
I'm building a DBAL, and I want the class constructor to automatically initiate a connection.

Thanks for any input. :D

Posted: Fri Sep 30, 2005 6:17 pm
by feyd
yes, you can do such a thing.

Code: Select all

// php 4
class foo {
  function foo($arg1, $arg2, $arg3) {
    // TODO add functionality
  }
}

// php 5
class bar {
  function __construct($arg1, $arg2, $arg3) {
    // TODO add functionality
  }
}

Posted: Fri Sep 30, 2005 6:22 pm
by Maugrim_The_Reaper
You need to read the manual ;)

Yes, you can pass parameters as stated. They are passed to the constructor method of your class (which is run on instantiation of the class to an object). The constructor can do whatever it wants with them.

Posted: Fri Sep 30, 2005 6:27 pm
by Sphenn
Awesome!!

I did read the manual, but must have missed this. Thanks again. :D

Posted: Fri Sep 30, 2005 7:56 pm
by Jenk