webForumDet fria alternativet

PHP och objektorientering...

2 svar · 358 visningar · startad av swede

swedeMedlem sedan feb. 2004230 inlägg
#1

Sitter och leker lite med PHP och objektorientering...

Har skapat en liten klass som följer:

class book {
   private $ISBN;
   private $author;

   function getISBN() {
     return $this->$ISBN;
   }

   function setISBN($ISBN){
     $this->$ISBN = $ISBN;
   }

   function getAuthor(){
     return $this->$author;
   }

   function setAuthor($author){
     $this->$author = $author;
   }
 }

Sedan försöker jag använda den såhär:

  include 'data.php';

  $books = new book();
  $books->setAuthor("johan");
  $books->setISBN("1112");

  echo $books->getAuthor();

Men då får jag följande fel meddelande:

PHP Notice:  Undefined variable: author in /test/include/data.php on line 16
PHP Notice:  Undefined property:  book::$ in /test/include/data.php on line 16

Vad är detjag har tänkt fel?

PeeerMedlem sedan mars 20025 907 inlägg
#2
class book {
   private $ISBN;
   private $author;

   function getISBN() {
     return $this->ISBN;
   }

   function setISBN($ISBN){
     $this->ISBN = $ISBN;
   }

   function getAuthor(){
     return $this->author;
   }

   function setAuthor($author){
     $this->author = $author;
   }
}

Dvs inga $ framför variablerna inom klassen.

swedeMedlem sedan feb. 2004230 inlägg
#3

TACK!!!

127 ms totalt · 3 externa anrop · v20260731065814-full.0e50b1ef
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
124 ms — hämta tråd, inlägg och bilagor (db)