PHP5中使超类对象成为子类对象

发布于 2024-08-09 03:36:28 字数 1831 浏览 3 评论 0原文

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

?>

我想通过将 A 的对象传递给 B 的构造函数来创建 B 的对象,如您所见,我无法重新分配 $this 变量。我不允许修改 A 类,当 A 中有很多属性时,在 B 的构造函数中做这样的事情对我来说会很乏味:

 public function __construct(A $a){

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

  $this->myProperty4='some value';

 }

我的问题是,如何使用 A 类安全地创建 B 类的对象A 的对象需要最少的编码?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦屿孤独相伴 2024-08-16 03:36:28
class A
{
  public $property = 'Foobar';
}

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

我错过了什么吗?听起来您正试图强迫 OOP 做一些它不打算做的事情,或者您在理解继承方面遇到困难。

类 A 中的每个公共或受保护的方法和属性都可以在类 B 中使用。可以通过直接引用它(如我的示例中),也可以使用 Parent:: 语法。

编辑

(作者澄清的问题)

如果 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;
    }
  }
}
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;
    }
  }
}
海拔太高太耀眼 2024-08-16 03:36:28

既然B扩展了A,为什么不一开始就创建B呢?如果您需要初始化一些额外的属性,您可以像这样覆盖构造函数:

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

}

如果这还不够好,那么您可能需要看看反射。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文