2 Replies - 2389 Views - Last Post: 04 November 2008 - 05:35 AM Rate Topic: -----

#1 dawemu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-August 08

PHP overload constructer code

Post icon  Posted 03 November 2008 - 08:15 PM

HI all. i'm a beginner to PHP. so i need some help from you to this problem. actually i dont know how to create overload constructors. so please help me to correct this code.
Thanks


<?php

/*
* Class Name - Search Class
* Author - Damith Mulleriyawa
* Date - 04/11/2008
* Version - 1.0
* Description - Search controller
*/

class Search {

//private attributes for the class
private $studentID;
private $studentFirstName;
private $studentLastName;
private $date;
private $timeIn;
private $timeOut;
private $staffID;
private $staffFirstName;
private $staffLastName;


//defult constructor
public function __construct() {
$num = func_num_args();
$args = func_get_args();
switch($num){
case 0:
$this->__call('__construct0', null);
break;
case 1:
$this->__call('__construct1', $args);
break;
default:
throw new Exception();
}
}

}

//constructor
public function __construct1($studentID,$studentFirstName,$studentLastName, $date, $timeIn, $timeOut) {
$this->studentID = $studentID;
$this->studentFirstName = $studentFirstName;
$this->studentLastName = $studentLastName;
$this->date = $date;
$this->timeIn = $timeIn;
$this->timeOut = $timeOut;

}

public function __construct1($staffID, $staffFirstName, $staffLastName) {
$this->studentID = $studentID;
$this->staffFirstName = $staffFirstName;
$this->staffLastName = $staffLastName;

}

public function getStudentID()
{
return $this->studentID;
}

public function setStudentID($studentID)
{
$this->studentID = $studentID;
}

public function getStudentFirstName()
{
return $this->studentFirstName;
}

public function setStudentFirstName($studentFirstName)
{
$this->studentFirstName = $studentFirstName;
}

public function getStudentLastName()
{
return $this->studentLastName;
}

public function setStudentLastName($studentLastName)
{
$this->studentLastName = $studentLastName;
}


public function getDate()
{
return $this->date;
}

public function setDate($date)
{
$this->date = $date;
}

public function getTimeIn()
{
return $this->timeIn;
}

public function setTimeIn($timeIn)
{
$this->timeIn = $timeIn;
}

public function getTimeOut($timeOut)
{
return $this->null;
}

public function setTimeOut($timeOut)
{
$this->timeOut = $timeOut;
}

public function getStaffID()
{
return $this->staffID;
}

public function setStaffID($staffID)
{
$this->staffID = $staffID;
}

public function getStaffFirstName()
{
return $this->staffFirstName;
}

public function setStaffFirstName($staffFirstName)
{
$this->staffFirstName = $staffFirstName;
}

public function getStaffLastName()
{
return $this->staffLastName;
}

public function setStaffLastName($staffLastName)
{
$this->staffLastName = $staffLastName;
}







}//end of class Attendance Record


?>

Is This A Good Question/Topic? 0
  • +

Replies To: PHP overload constructer code

#2 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: PHP overload constructer code

Posted 04 November 2008 - 02:40 AM

- Please put your code between "code" tags
- Please indent your code correctly to make it readable and more importantly more understandable.
- Please report any errors ;)

Back to your question:
Bacause PHP cannot parse a function (and constructor) multiple times, it is not possible to write multiple constructors with a different number of arguments. You can solve it by passing an associative array (param => value) to a function, so it has a fixed-length parameter (just one).
For an example and discussions, click here.

The solutions you have should work as well. I loaded your file and:
Parse error: syntax error, unexpected T_PUBLIC in C:\xampp\htdocs\search.php on line 44This
This is caused by the } on line 41, remove it. (You could have seen it by using indentation)

Next:
Fatal error: Cannot redeclare Search::__construct1() in C:\xampp\htdocs\search.php on line 54
You copied it to fast, rename one of both :)

Done. I hope this works for you!
Was This Post Helpful? 0
  • +
  • -

#3 dawemu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-August 08

Re: PHP overload constructer code

Posted 04 November 2008 - 05:35 AM

View PostHary, on 4 Nov, 2008 - 01:40 AM, said:

- Please put your code between "code" tags
- Please indent your code correctly to make it readable and more importantly more understandable.
- Please report any errors ;)

Back to your question:
Bacause PHP cannot parse a function (and constructor) multiple times, it is not possible to write multiple constructors with a different number of arguments. You can solve it by passing an associative array (param => value) to a function, so it has a fixed-length parameter (just one).
For an example and discussions, click here.

The solutions you have should work as well. I loaded your file and:
Parse error: syntax error, unexpected T_PUBLIC in C:\xampp\htdocs\search.php on line 44This
This is caused by the } on line 41, remove it. (You could have seen it by using indentation)

Next:
Fatal error: Cannot redeclare Search::__construct1() in C:\xampp\htdocs\search.php on line 54
You copied it to fast, rename one of both :)

Done. I hope this works for you!


Hi hary
Thank you very much for help me. i hope to contact with u via dreamincode
thanks buddy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1