Jump to content

[SOLVED] [Beginner] Cannot access empty property


AV

Recommended Posts

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.