Quick OOP Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sphenn
Forum Commoner
Posts: 48
Joined: Sun Jul 17, 2005 8:08 pm
Location: Winnipeg, MB

Quick OOP Question

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
  }
}
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
Sphenn
Forum Commoner
Posts: 48
Joined: Sun Jul 17, 2005 8:08 pm
Location: Winnipeg, MB

Post by Sphenn »

Awesome!!

I did read the manual, but must have missed this. Thanks again. :D
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Post Reply