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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.