multiple constructors php5

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
jonathant
Forum Commoner
Posts: 32
Joined: Sat Jan 07, 2006 3:13 pm

multiple constructors php5

Post 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
}
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
jonathant
Forum Commoner
Posts: 32
Joined: Sat Jan 07, 2006 3:13 pm

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

The manual contains all you need to know..

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