Domanda

<?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

?>

Vorrei creare un oggetto di B passando l'oggetto di A al costruttore di B, come puoi vedere, non posso riassegnare $ questa variabile. Non mi è permesso modificare la classe A, quando ci sono molte proprietà in A, sarebbe noioso fare cose come queste nel costruttore di B:

 public function __construct(A $a){

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

  $this->myProperty4='some value';

 }

La mia domanda è che, come posso creare in sicurezza un oggetto di classe B usando un oggetto A con una quantità minima di codifica?

È stato utile?

Soluzione

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

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

Mi sto perdendo qualcosa? Sembra che tu stia cercando di forzare OOP a fare qualcosa che non è destinato a fare, o hai problemi a capire l'eredità.

Ogni metodo e proprietà pubblica o protetta della classe A è disponibile nella classe B. O facendo riferimento direttamente ad esso (come nel mio esempio) o usando la sintassi parent ::.

Modifica

(domanda chiarita dall'autore)

Se le proprietà della classe A sono accessibili, è possibile utilizzare qualcosa di simile al seguente per copiarle nella classe 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;
    }
  }
}

Altri suggerimenti

Dato che B estende A, perché non creare B per cominciare? Se devi inizializzare alcune proprietà extra, puoi scavalcare il costruttore in questo modo:

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

}

Se non è abbastanza buono, allora potresti voler guardare Reflection.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top