views:

88

answers:

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

?>

I'd like to create a B's object by passing an A's object to B's constructor, as you can see, I cannot re-assign the $this variable. I am not allowed to modify class A, when there are many properties in A, it'd be tedious for me to do things like this in B's constructor:

 public function __construct(A $a){

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

  $this->myProperty4='some value';

 }

My question is that, how can I safely create an object of class B using an A's object with minimal amount of coding?

+2  A: 
class A
{
  public $property = 'Foobar';
}

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

Am I missing something? It sounds like you're trying to force OOP to do something it's not intended to do, or you're having trouble understanding inheritance.

Every public or protected method and property from class A is available in class B. Either by directly referencing it (as in my example) or by using the parent:: syntax.

EDIT

(Author clarified question)

If class A's properties are accessible, you could use something like the following to copy them down to class 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;
    }
  }
}
Mike B
since you've over-ridden A's constructor, B won't set the properties set by A's constructor. http://ca.php.net/__construct
dnagirl
@dnagirl In my example, A does not have a constructor. If it did, all you would need is parent::__construct() in B's contructor.
Mike B
I think I'm missing something too.
Tim Post
sorry, my example was poorly written, now I have made some changes, $a is not created by me, it's actually returned from other functions and its properties may have already been altered, probably different from those initialized in the constructor
bobo
A: 

Since B extends A, why not just create B to begin with? If you need to initialize some extra properties, you can over-ride the constructor like this:

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

}

If that's not good enough, then you might want to look at Reflection.

dnagirl