سؤال

<?php

    class A{

     //many properties
     protected $myProperty1;
     protected $myProperty2;
     protected $myProperty3; 

     public function __construct(){
      $this->myProperty1='some value';
      $this->myProperty2='some value';
      $this->myProperty3='some value';
     }

     public function getProperty1(){
      return $this->myProperty1;
     }
     public function getProperty2(){
      return $this->myProperty2;
     }
     public function getProperty3(){
      return $this->myProperty3;
     }

     //edited: I added some setters, meaning that the object returned from the functions may already have these properties altered

     public function setProperty1($p){
      $this->myProperty1=$p;
     }
     public function setProperty2($p){
      $this->myProperty2=$p;
     }
     public function setProperty3($p){
      $this->myProperty3=$p;
     }


    }

    class B extends A{

     private $myProperty4;

     public function __construct(A $a){
      $this=$a; //this line has error,it says $this cannot be re-assigned
      $this->myProperty4='some value';
     }

     public function getProperty4(){
      return $this->myProperty4;
     }   
    }

   //$a = new A();
   $a = someClass::getAById(1234); //edited: $a is returned by a function (I cannot modify it)
   $b= new B($a); //error

?>

وأود أن إنشاء الكائن B عن طريق تمرير الكائن على ألف لمنشئ B، كما ترون، لا أستطيع إعادة تعيين $ هذا المتغير. لم يسمح لي لتعديل الدرجة A، عندما تكون هناك العديد من الخصائص في A، انها تريد ان تكون مملة بالنسبة لي أن أفعل أشياء من هذا القبيل في منشئ باء:

 public function __construct(A $a){

  parent::__construct();
  $this->myProperty1=$a->getProperty1(); 
  $this->myProperty2=$a->getProperty2();
  $this->myProperty3=$a->getProperty3();

  $this->myProperty4='some value';

 }

وسؤالي هو، كيف يمكنني بأمان إنشاء كائن من الفئة B باستخدام كائن من A مع الحد الأدنى من الترميز؟

هل كانت مفيدة؟

المحلول

class A
{
  public $property = 'Foobar';
}

class B extends A
{
  public function __construct()
  {
    echo $this->property; // Foobar
  }
}

وأنا شيء مفقود؟ هذا يبدو وكأنه كنت تحاول إجبار OOP أن تفعل شيئا انها لا تعتزم القيام به، أو كنت تواجه مشكلة في فهم الإرث.

وكل أسلوب عام أو المحمية والممتلكات من فئة A متوفر في الفئة B. إما عن طريق الرجوع مباشرة عليه (كما هو الحال في بلدي على سبيل المثال) أو باستخدام الوالد :: بناء الجملة.

تحرير

و(الكاتب توضيح مسألة)

إذا خصائص فئة A ويمكن الوصول إليها، هل يمكن استخدام شيء من هذا القبيل ما يلي لنسخها وصولا الى الدرجة B

class B
{
  public function __construct()
  {
    $a = new A(); // Or however A is instantiated
    foreach(get_object_vars($a) as $key => $value)
    {
      $this->$key = $value;
    }
  }
}

نصائح أخرى

ومنذ B يمتد A، لماذا لا مجرد خلق B أن تبدأ؟ إذا كنت بحاجة إلى تهيئة بعض خصائص إضافية، يمكنك الإفراط في ركوب منشئ مثل هذا:

class B extends A {
    public function __construct(){
      parent::__construct(); //calls A's constructor
      $this->Bproperty='somevalue';
    }

}

إذا هذا ليس جيدا بما فيه الكفاية، فإنك قد ترغب في النظر في التفكير.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top