Page 1 of 1

multiple constructors php5

Posted: Sat Jan 14, 2006 3:09 pm
by jonathant
Is it possible to have multiple constructors in one class? Is this what overloading is? I want to be able to instantiate the object in different ways, based on how many arguments are passed.

EX:

Code: Select all

<?php
class User
{
	public $username;
	public $firstname;
	public $lastname;
	
	public function _construct($id) // this one should get data
	{
		$query="SELECT * FROM users WHERE id='$id'";
		$result=mysql_query($query);
		$row=mysql_fetch_assoc($result);
		$this->username=$row['username'];
		$this->firstname=$row['firstname'];
		$this->lastname=$row['lastname'];
	}
	
	public function _construct($username, $firstname, $lastname)  // this one should add data
	{
		$query="INSERT INTO users VALUES(NULL, $username, $firstname, $lastname)";
		$result=mysql_query($query);
		$this->username=$username;
		$this->firstname=$firstname;
		$this->lastname=$lastname;
	}
}
?>
I have been doing the following to emulate this in php4, which can still be used in php5, but I was wanting to know if there is any way to incorporate the _construct method, or if it is even worthwhile.

Code: Select all

function User()
{
	$name = "User".func_num_args();
	$arg_list = func_get_args();
	$num_args = func_num_args();
	switch($num_args) 
	{
		case 1:
		$this->$name($arg_list[0]);
		break;
		case 3:
		$this->$name($arg_list[0],$arg_list[1],$arg_list[2]);
		break;
		default: echo "Wrong number of parameters on User constructor!";
	}
}

function User1($id)
{
     // code to get user data from db
}

function User3($username, $firstname, $lastname)
{
     // code to insert data into db
}

Posted: Sat Jan 14, 2006 4:10 pm
by alex.barylski
Thats overloading, yes. :)

Having not ever used PHP5 I can't say whether PHP supports overloading...even if it does...it might not allow that with constructors...no idea

I'm sure someone will jump in and tell you soon enough...

However in the mean time...giver a try and see what happens :)

Posted: Sat Jan 14, 2006 4:19 pm
by jonathant
The second method using the func_get_args() and all of that stuff works in PHP5. It just looks disorganized, and if I need to add a new argument to the constructor, I also have to edit the "real" constructor to accomodate, and change the "secondary" constructor's name. It isn't a huge deal because I don't often change the number of arguments to the function, but it does look and feel like a bad practice.

Posted: Sat Jan 14, 2006 4:34 pm
by alex.barylski
jonathant wrote:The second method using the func_get_args() and all of that stuff works in PHP5. It just looks disorganized, and if I need to add a new argument to the constructor, I also have to edit the "real" constructor to accomodate, and change the "secondary" constructor's name. It isn't a huge deal because I don't often change the number of arguments to the function, but it does look and feel like a bad practice.
It's bad practice I agree...

I'd maybe use it in a pinch, but i'm not a fan of ad hoc hacks...

Actually I have used that technique, but for framework, so I was certain the function signature wasn't going to change...

In PHP4 I was likely just have 2 or more functions with similar names and append numbers to the end...

Code: Select all

createObject1($p1, $p2, $p3)
createObject2($p1)
This way, when porting over to PHP5, it's easy to just drop the numbers and viola your PHP overloading in version 5...

Also it has the added benefit of allowing auto doc applications like doxygen to properly generate docs without any comment stubs...

EDIT: Probably won't work well for constructors though :? sorry didn't think of that :oops:

My two cents :)

Cheers :)

Posted: Sat Jan 14, 2006 8:07 pm
by Jenk
The manual contains all you need to know..

http://www.php.net/manual/en/language.o ... oading.php