OOP in PHP
Posted: Mon Mar 01, 2010 3:55 pm
I am a Java Developer (and my PHP skills are really bad), so please bare with me.
I was trying to do some OOP in PHP but I am getting some errors.
I have 2 classes "Individual" and "IndividualDAO"
So far so good...
but when in let say "index.php" I write:
So the above code should print the First Name, right ?
But PHP throws this error:
Catchable fatal error: Object of class Individual could not be converted to string in C:\xampp\htdocs\...\Individual.php on line 26
line 26 is the line of Individual.php which defines "getFirstName()" function.
What I am doing wrong and how to solve it ?
Thanks.
I was trying to do some OOP in PHP but I am getting some errors.
I have 2 classes "Individual" and "IndividualDAO"
Code: Select all
class Individual {
private $id;
private $fn;
public function Individual($id, $fn) {
$this->id = $id;
$this->fn = $fn;
}
// getters...
public function getID() { return $this->id; }
public function getFirstName() { return ucfirst($this->fn); } // This is line 26
// setters....
public function setFirstName($fn) { $this->fn = $fn; }
}
Code: Select all
class IndividualDAO {
public function IndividualDAO() {
}
public static function getIndividualByID($id) {
$ind = null;
$r = mysql_fetch_array(mysql_query("select * from individuals where id=".$id));
$ind = new Individual($r["id"], $r["fn"]);
return $ind;
}
}
but when in let say "index.php" I write:
Code: Select all
$individualDAO = new IndividualDAO();
$ind = $individualDAO->getIndividualByID(1);
echo $ind->getFirstName(); // this should print the first name
But PHP throws this error:
Catchable fatal error: Object of class Individual could not be converted to string in C:\xampp\htdocs\...\Individual.php on line 26
line 26 is the line of Individual.php which defines "getFirstName()" function.
What I am doing wrong and how to solve it ?
Thanks.