OOP in PHP

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
rizjav_86
Forum Newbie
Posts: 16
Joined: Wed Feb 24, 2010 10:09 am

OOP in PHP

Post by rizjav_86 »

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"

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;
        }
    }
 
So far so good...

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
 
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.
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: OOP in PHP

Post by lshaw »

What is in your database? because this code works fine on my local machine, and doesnt throw any errors
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: OOP in PHP

Post by AbraCadaver »

lshaw wrote:What is in your database? because this code works fine on my local machine, and doesnt throw any errors
For me too. Is either a version bug or some code that you haven't posted is causing the problem.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: OOP in PHP

Post by requinix »

I don't see anything wrong either.

Now, if it was

Code: Select all

return ucfirst($this);
I would understand.
rizjav_86
Forum Newbie
Posts: 16
Joined: Wed Feb 24, 2010 10:09 am

Re: OOP in PHP

Post by rizjav_86 »

Weird, works fine at my home pc too, don't know why it wasn't working at work computer !
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: OOP in PHP

Post by josh »

You'd need to show the contents of the array from the query, and post your PHP version.
Post Reply