$ This-> a-> b-> c-> d php bir üst sınıftan yöntemleri çağırmadan

3 Cevap php

Merhaba ben sınıflar üzerinde bir şey yapmak istiyorum

i biri benim tüm sınıf üzerinde genişletilir bir süper sınıf yapmak istiyorum

            ____  database class
           /
chesterx _/______  member class
          \
           \_____  another class

ben böyle veritabanı sınıfında yöntemi çağırmak istiyorum

$this->database->smtelse();



class Hello extends Chesterx{

  public function ornekFunc(){
     $this->database->getQuery('popularNews');
     $this->member->lastRegistered();
  }

}

ve i herhangi bir sınıf için benim süper sınıfını genişletmek zaman onun ana sınıfı adıyla bir yöntemi çağırmak istiyorum

3 Cevap

Ben size son cümle ile ne demek pek emin değilim ama bu mükemmel geçerlidir:

class Chesterx{
 public $database, $member;
 public function __construct(){
   $this->database = new database; //Whatever you use to create a database
   $this->member = new member;
 }
}

Singleton deseni düşünün - genellikle veritabanı etkileşimleri için daha iyi uyuyor. http://en.wikipedia.org/wiki/Singleton_pattern.

you could also consider using methods to get the sub-Objects The advantage would be that the objecs are not initialized until they are need it, and also provides a much more loosely coupled code that lets you change the way the database is initialized more easy.

class Chesterx{
   public $database, $member;

   public function getDatabase() {
       if (!$this->database ) {
           $this->database = new database; //Whatever you use to create a database
       }
       return $this->database;
   }

   public function getMember() {
       if (!$this->member) {
           $this->member = new member;
       }
       return $this->member;
   }

}