PHP Çocuk sınıf erişen Ebeveyn değişkeni sorunu

4 Cevap php

Ben bu sınıf var:

Class Username {
protected $id;
protected $username;
protected $contact_information;

     private __construct($id) {
       $this->id = (int) $id; // Forces it to be a string
       $contact_information = new ContactInformation($this->id);
     }
}

Class ContactInformation extends Username {
    protected $mobile;
    protected $email;
    protected $nextel_id;
    .....
}

Benim sorunum: Ben İletişim Bilgileri üzerinde $ id $ ve kullanıcı adı (ve diğer değişkenler çok) erişmek istediğiniz, ancak ebeveyn :: ya $ this-> değil iş, i "yeni İletişim Bilgileri yapılacak her şey gibi görünüyor ....) PHP bir "yeni bir Kullanıcı Adı". kimden GÜNCEL değerlerine erişmek için herhangi bir şans yaratır?

Teşekkürler

4 Cevap

Neden adı yapıcı özel nedir? Eğer imkansız bir Kullanıcı Adı oluşturmak için yapmak demek, adı sınıf soyut olun. Ayrıca, ana sınıfından yeni bir iletişim bilgileri DEĞIL. İşte koymak için başka bir yolu:

abstract class Username {
   protected $id;
   protected $username;

   public __construct($id) {
      $this->id = (int) $id; // Forces it to be a string
   }
}

class ContactInformation extends Username {
    protected $mobile;
    protected $email;
    protected $nextel_id;
    public __construct($id, $mobile, $email, $nextel_id) {
       parent::__construct($id)
       $this->mobile = $mobile;
       ....
    }
}

Şimdi, bunun yerine (artık imkansız olan) doğrudan Adı başlatmasını, bunun yerine bir İletişim Bilgileri oluşturun. İletişim Bilgileri sonra kendi yapıcısındaki adı kurucu çağırır.

Parent :: yöntem size sadece alt sınıfta veya benzeri statik değişkenleri geçersiz kılınmış ana yöntemlerine erişmek için kullanılır:

class Base
{
    protected static $me;

    public function __construct ()
    {
        self::$me = 'the base';
    }

    public function who() {
        echo self::$me;
    }
}

class Child extends Base
{
    protected static $me;

    public function __construct ()
    {
        parent::__construct();
        self::$me = 'the child extends '.parent::$me;
    }

    // until PHP 5.3, will need to redeclare this
    public function who() {
        echo self::$me;
    }
}

$objA = new Base;
$objA->who(); // "the base"

$objB = new Child;
$objB->who(); // "the child extends the base"

Muhtemelen uygun bir alt sınıfı istiyorum. Ayrıca sonsuz bir döngü oluştururken temel sınıfın yapıcısı bir alt sınıfı oluşturmak etmeyin, bu baş aşağı (gevşek bağlanması, vb) OOP en iyi uygulamaları her türlü döner. (Yeni İletişim Bilgileri () yeni bir İletişim Bilgileri () oluşturur adı kurucu çağırır ...).

: Bir alt sınıfı, böyle bir şey istiyorum

/**
 * Stores basic user information
 */
class User
{
    protected $id;
    protected $username;

    // You could make this protected if you only wanted
    // the subclasses to be instantiated
    public function __construct ( $id )
    {
        $this->id = (int)$id; // cast to INT, not string

        // probably find the username, right?
    }
}

/**
 * Access to a user's contact information
 */
class ContactInformation extends User
{
    protected $mobile;
    protected $email;
    protected $nextel;

    // We're overriding the constructor...
    public function __construct ( $id )
    {
        // ... so we need to call the parent's
        // constructor.
        parent::__construct($id);

        // fetch the additional contact information
    }
}

Ya da bir temsilci kullanabilirsiniz, ancak daha sonra İletişim Bilgileri yöntemleri Kullanıcı Adı özelliklerine doğrudan erişim olmazdı.

class Username
{
    protected $id;
    protected $contact_information;

    public function __construct($id)
    {
        $this->id = (int)$id;
        $this->contact_information = new ContactInformation($this->id);
    }
}

class ContactInformation // no inheritance here!
{
    protected $user_id;
    protected $mobile;

    public function __construct($id)
    {
        $this->user_id = (int)$id;
        // and so on
    }
}

Oluşturduğunuzda, ilk bir ContactInformation, aynı zamanda tüm özel olmayan özellikleri ve yöntemleri içerir Username. Edilir Ayrı bir Username örneği gerekmez.

Class Username {
    protected $id;
    protected $username;

    protected __construct($id) {
        $this->id = (int) $id; // Forces it to be a string
    }
}

Class ContactInformation extends Username {
    protected $mobile;
    protected $email;
    protected $nextel_id;
    // Pretend that these are here because they're defined in my parent
    //protected $id;
    //protected $username;

    public __construct($id) {
        parent::__construct($id);
        echo $this->id; //Should echo 1
    }
}

Tüm alanlar korunur Ancak, bu işe gitmiyor:

$contact_information = new ContactInformation(1); // Works fine
echo $contact_information->id;
// Whoops, visibility error because id isn't public

Ben doğru anlamak, o nesnenin içerdiği bir nesneden bir nesnenin özelliklerine erişmek istiyor? Eğer bu doğru ise, burada onun yapılır nasıl:

class A {

  // These are the properties you want to access from the child object
  public $property_a;
  public $property_b;
  public $property_c;

  // This is the child object variable
  public $child_object;

  public function __construct( ) {

    // Pass 'this' into the child so that the child has a reference back to the parent
    $this->child_object = new B($this);
  }
}

class B {

  // Holds a reference to the parent object
  protected $parent_object;

  public function __construct( $object ) {

    // Remember the reference to the parent object
    $this->parent_object = $object;
  }

  // Just a Demonstration Method
  public print_parent_property_a()
  {
    // Reach into the referred parent object, and get it's property
    print $this->parent_object->property_a;
  }
}

Yapmanız olsaydı Yani:

$my_object = new A();
$my_object->property_a = 'test_value';
$my_object->child_object->print_parent_property_a();

Sen 'test_value' olsun istiyorum

Çocuk onlara erişebilirsiniz böylece kamu olmak üzere üst sınıf özelliklerini gerekir ki o, sizin örnekte biraz farklı.

Eğer onları explicity klonlamak sürece bu tüm eserler, çünkü PHP, nesneleri her zaman başvuruya göre iletilir.