AV Posted November 17, 2007 Share Posted November 17, 2007 Hi all, I started to learning OOP and just on start I got an error: Fatal error: Cannot access empty property in (...)books.php on line 16 The php code: class Books{ var $cena; var $tytul; function setCena($par) { echo $this->$cena = $par; } function getCena(){ echo $this->$cena.'<br />'; } function setTytul($par){ echo $this->$tytul = $par; } function getTytul(){ echo $this->$tytul.'<br />'; } } $fizyka = new Books; $chemia = new Books; $matematyka = new Books; $fizyka->setTytul("Fizyka dla kazdego - cz. I"); $fizyka->setCena("40zl"); $chemia->setTytul("Chemia wokol nas"); $chemia->setCena("30zl"); $matematyka->setTytul("Pitagoras i ja"); $matematyka->setTytul("35zl"); //WYSWIETLAMY KSIAZKI $fizyka->getTytul(); echo"||"; $fizyka->getCena(); Tytul=title, Cena=price What do I do wrong? Quote Link to comment Share on other sites More sharing options...
coder_ Posted November 17, 2007 Share Posted November 17, 2007 function setCena($par) { echo $this->cena = $par; } Change your functions to look like this. You don't use "$" when you are pointing to some object. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted November 17, 2007 Share Posted November 17, 2007 coder_ is correct. when you reference a class member, "$this->" replaces the $ Also your methods get and set are kinda unnecessary (in their current state) as you can access those members because they are public $b = new Books(); $b->cena = 'some cena'; //this is the same as your setCena() method And if at all possible, I'd say learn PHP OOP through PHP5 Quote Link to comment Share on other sites More sharing options...
AV Posted November 17, 2007 Author Share Posted November 17, 2007 Thank you very much guys! Do you have any clearly written tutorials for OOP in PHP5? Quote Link to comment Share on other sites More sharing options...
coder_ Posted November 18, 2007 Share Posted November 18, 2007 Her you can learn a lot: http://www.php.net/manual/en/language.oop5.php read my signature... php.net &&google.com can solve anything.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.