Bu PHP sınıfı neden çalışmıyor

4 Cevap

görmek

class Browser{

var $type = "";

public function e(){

return $this->type;
}

}

zaman kullanımı

$b = new Browser('human');

echo $b->e();

ve ben görünmez yazın Maen ve ben yeni ArchiveBrowser (var tipi) gibi insan yapmak;

4 Cevap

  • Bir fonksiyon adı olarak "echo" kullanamazsınız.
  • Bir kurucu uğraşıyorsun. Bu deneyin

.

class Browser{
    var $type = "";

    function __construct($type){
         $this->type = $type;
    }

    public function echo_type(){
        return $this->type;
   }
}

echo ayrılmış bir sözcüktür. Ayrıca class Browser denir ama ArchiveBrowser başlatmasını ediyoruz.

class Browser 
{
   // Always declare whether a variable is public or private
   private $type = null;
   // A constructor - gets excecuted every time you create a class
   public function __construct($type)
   {
      // Note that $type here is not the same as $type above
      // The $type above is now $this->type

      $this->type = $type; // Store the type variable
   }

   // Your function e()
   public function e ()
   {
       return $this->type;
   }

   // __toString() method. (May also be useful)
   // it gets excecuted every time you echo the class, see below.
   public function __toString ()
   {
      return $this->e(); // I chose to return function e() output here
   }

}

Kullanım örnekleri:

$b = new Browser('Human'); // Note, that this executes __construct('Human');
echo $b->e();              // Echos 'Human'

$b = new Browser('Lolcat'); // Excecutes __construct('Lolcat');
echo $b->__toString();      // Echos 'Lolcat'
echo $b;                    // Echos 'Lolcat', equivalent to statement above

//Also:
$c = (string) $b;
echo $c;                    // Echos 'Lolcat'

ArchiveBrowser Tarayıcı uzatmak gerekir ya da sen ArchiveBrowser yerine tarayıcı kullanmalısınız.